javascript
Spring的AOP-AspectJ注解方式
目錄
Spring的AOP-AspectJ注解方式
1.創建類,在類里面定義方法
2.創建增強類
3.進行通知的配置
(1)在Spring 配置文件中,開啟直接掃描
(2)使用注解創建User 和 UserProxy 對象
(3)在增強類上面添加注解@Aspect
(4)在Spring 配置文件中開啟生成代理對象
4.配置不同類型的通知
(1)在增強類的里面,在作為通知方法上面添加通知類型注解,使用切入點表達式配置
5.測試:
? ? ? ? ?全部進行測試:
? ? ? ? ?測試結果:
觀察:
7.如果有多個增強類同時對一個方法進行增強,可以設置增強類的優先級
(1)在增強類上面添加注解@Order(數字類型值)
??結果:
Spring的AOP-AspectJ注解方式
(需要看上一章把xjar包都導入才可以執行以下)
1.創建類,在類里面定義方法
新建一個aop_annotation包
在aop_annotation包內新建一個User類
User類代碼如下:
package com.lbj.spring5.aop_annotation;public class User {public void add(){System.out.println("add。。。");} }?
2.創建增強類
package com.lbj.spring5.aop_annotation;//增強的類 public class UserProxy {//前置通知public void before(){System.out.println("before");} }?
3.進行通知的配置
(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)使用注解創建User 和 UserProxy 對象
一個是被增強的User類
一個是增強的UserProxy類
?
(3)在增強類上面添加注解@Aspect
?
(4)在Spring 配置文件中開啟生成代理對象
?
?
4.配置不同類型的通知
(1)在增強類的里面,在作為通知方法上面添加通知類型注解,使用切入點表達式配置
package com.lbj.spring5.aop_annotation;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;//增強的類 @Component @Aspect //生成代理對象 public class UserProxy {//前置通知//@Before注解表示作為前置通知@Before(value = "execution(* com.lbj.spring5.aop_annotation.User.add(..))")public void before(){System.out.println("before");} }?
5.測試:
注意事項:當我們使用了maven搭建項目的時候,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);//調用對象的方法user.add();} }測試結果:?
?
全部進行測試:
package com.lbj.spring5.aop_annotation;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;//增強的類 @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...");}//環繞通知@Around(value = "execution(* com.lbj.spring5.aop_annotation.User.add(..))")public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{System.out.println("beforeAround...");//被增強的方法執行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);//調用對象的方法user.add();} }?
測試結果:
?
?
手動測試異常類:
明顯10/0 是異常的
package com.lbj.spring5.aop_annotation;import org.springframework.stereotype.Component;//被增強的類 @Component public class User {public void add(){int i=10/0;System.out.println("add...");} }測試結果:
?
觀察:
after不管有沒有異常都會執行
afterReturning 只要出現異常就不會執行
?
6.相同的切入點抽取
作用是:把一些重復寫的寫成公共部分
?
進行方法調用即可
?
?
7.如果有多個增強類同時對一個方法進行增強,可以設置增強類的優先級
(1)在增強類上面添加注解@Order(數字類型值)
數字類型值:越小,優先等級越高
對比:?
?
結果:
?
?
8.完全注解開發:
(1)創建配置類,不需要創建xml配置文件
@Configuration
@ComponentScan(basePackages={''包路徑''})
@EnableAspectJAutoProsy(proxyTargetClass=true)
public class ConfigAop{
}
?
總結
以上是生活随笔為你收集整理的Spring的AOP-AspectJ注解方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA进阶教学之(Object类中的e
- 下一篇: Spring的AOP-底层原理