javascript
Spring的AOP-AspectJ注解方式
目錄
Spring的AOP-AspectJ注解方式
1.創(chuàng)建類,在類里面定義方法
2.創(chuàng)建增強(qiáng)類
3.進(jìn)行通知的配置
(1)在Spring 配置文件中,開啟直接掃描
(2)使用注解創(chuàng)建User 和 UserProxy 對象
(3)在增強(qiáng)類上面添加注解@Aspect
(4)在Spring 配置文件中開啟生成代理對象
4.配置不同類型的通知
(1)在增強(qiáng)類的里面,在作為通知方法上面添加通知類型注解,使用切入點(diǎn)表達(dá)式配置
5.測試:
? ? ? ? ?全部進(jìn)行測試:
? ? ? ? ?測試結(jié)果:
觀察:
7.如果有多個增強(qiáng)類同時對一個方法進(jìn)行增強(qiáng),可以設(shè)置增強(qiáng)類的優(yōu)先級
(1)在增強(qiáng)類上面添加注解@Order(數(shù)字類型值)
??結(jié)果:
Spring的AOP-AspectJ注解方式
(需要看上一章把xjar包都導(dǎo)入才可以執(zhí)行以下)
1.創(chuàng)建類,在類里面定義方法
新建一個aop_annotation包
在aop_annotation包內(nèi)新建一個User類
User類代碼如下:
package com.lbj.spring5.aop_annotation;public class User {public void add(){System.out.println("add。。。");} }?
2.創(chuàng)建增強(qiáng)類
package com.lbj.spring5.aop_annotation;//增強(qiáng)的類 public class UserProxy {//前置通知public void before(){System.out.println("before");} }?
3.進(jìn)行通知的配置
(1)在Spring 配置文件中,開啟直接掃描
bean2.xml:
<?xml version="1.0" encoding="UTF-8"?> <!--名稱空間--> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--開啟注解掃描/開啟組件掃描--><context:component-scan base-package="com.lbj.spring5.aop_annotation"></context:component-scan> </beans>?
(2)使用注解創(chuàng)建User 和 UserProxy 對象
一個是被增強(qiáng)的User類
一個是增強(qiáng)的UserProxy類
?
(3)在增強(qiáng)類上面添加注解@Aspect
?
(4)在Spring 配置文件中開啟生成代理對象
?
?
4.配置不同類型的通知
(1)在增強(qiáng)類的里面,在作為通知方法上面添加通知類型注解,使用切入點(diǎn)表達(dá)式配置
package com.lbj.spring5.aop_annotation;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;//增強(qiáng)的類 @Component @Aspect //生成代理對象 public class UserProxy {//前置通知//@Before注解表示作為前置通知@Before(value = "execution(* com.lbj.spring5.aop_annotation.User.add(..))")public void before(){System.out.println("before");} }?
5.測試:
注意事項(xiàng):當(dāng)我們使用了maven搭建項(xiàng)目的時候,bean1.xml文件需要到resources文件里面才能被使用
測試代碼:
package com.lbj.spring5.test;import com.lbj.spring5.aop_annotation.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAop {@Testpublic void testAopAnnotation(){//加載配置文件ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");//得到對象,引入依賴User user = context.getBean("user", User.class);//調(diào)用對象的方法user.add();} }測試結(jié)果:?
?
全部進(jìn)行測試:
package com.lbj.spring5.aop_annotation;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;//增強(qiáng)的類 @Component @Aspect //生成代理對象 public class UserProxy {//前置通知//@Before注解表示作為前置通知@Before(value = "execution(* com.lbj.spring5.aop_annotation.User.add(..))")public void before(){System.out.println("before...");}//最終通知@After(value = "execution(* com.lbj.spring5.aop_annotation.User.add(..))")public void after(){System.out.println("after...");}//后置通知、返回通知@AfterReturning(value = "execution(* com.lbj.spring5.aop_annotation.User.add(..))")public void afterReturning(){System.out.println("afterReturning...");}//異常通知@AfterThrowing(value = "execution(* com.lbj.spring5.aop_annotation.User.add(..))")public void afterThrowing(){System.out.println("afterThrowing...");}//環(huán)繞通知@Around(value = "execution(* com.lbj.spring5.aop_annotation.User.add(..))")public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{System.out.println("beforeAround...");//被增強(qiáng)的方法執(zhí)行proceedingJoinPoint.proceed();System.out.println("afterAround...");} }測試類:
package com.lbj.spring5.test;import com.lbj.spring5.aop_annotation.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by 14811 on 2020/12/23.*/ public class TestAop {@Testpublic void testAopAnnotation(){//加載配置文件ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");//得到對象,引入依賴User user = context.getBean("user", User.class);//調(diào)用對象的方法user.add();} }?
測試結(jié)果:
?
?
手動測試異常類:
明顯10/0 是異常的
package com.lbj.spring5.aop_annotation;import org.springframework.stereotype.Component;//被增強(qiáng)的類 @Component public class User {public void add(){int i=10/0;System.out.println("add...");} }測試結(jié)果:
?
觀察:
after不管有沒有異常都會執(zhí)行
afterReturning 只要出現(xiàn)異常就不會執(zhí)行
?
6.相同的切入點(diǎn)抽取
作用是:把一些重復(fù)寫的寫成公共部分
?
進(jìn)行方法調(diào)用即可
?
?
7.如果有多個增強(qiáng)類同時對一個方法進(jìn)行增強(qiáng),可以設(shè)置增強(qiáng)類的優(yōu)先級
(1)在增強(qiáng)類上面添加注解@Order(數(shù)字類型值)
數(shù)字類型值:越小,優(yōu)先等級越高
對比:?
?
結(jié)果:
?
?
8.完全注解開發(fā):
(1)創(chuàng)建配置類,不需要創(chuàng)建xml配置文件
@Configuration
@ComponentScan(basePackages={''包路徑''})
@EnableAspectJAutoProsy(proxyTargetClass=true)
public class ConfigAop{
}
?
總結(jié)
以上是生活随笔為你收集整理的Spring的AOP-AspectJ注解方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA进阶教学之(Object类中的e
- 下一篇: 台式电脑耳机插孔在哪_一图教你学会电脑主