詳解在Spring中如何使用AspectJ來實現(xiàn)AOP
AspectJ 是通過注解來描述切點與增強的。
1 開發(fā)環(huán)境要求
因為要使用注解,所以請確保使用的 Java5.0 及以上版本。
引入 AspectJ 相關(guān)類庫:
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>${aopalliance.version}</version> </dependency>
2 編程方式
@Aspect//標識切面
public class PreRentAspect { /** * 增強邏輯 */ @Before("execution(* rent(..))")//定義切點與增強類型 public void beforeRent() { System.out.println("開始執(zhí)行租賃動作"); } }
這個切面只是一個普通的 POJO,只不過加了 @Aspect 注解。
@Before("execution(* rent(..))")
中的 @Before
表示增強類型是前置增強,它的內(nèi)容是 @AspectJ 切點表達式,這里表示的是在目標類的 rent() 方法上織入增強, rent() 可以包含任意入?yún)⒑腿我獾姆祷刂怠?/p>
帶 @Aspect
的類,通過注解與代碼,將切點、增強類型和增強的橫切邏輯整合到了一起,是不是很方便呀O(∩_∩)O哈哈~
單元測試:
AspectJProxyFactory factory = new AspectJProxyFactory(); //設置目標類 factory.setTarget(new User()); //添加切面類 factory.addAspect(PreRentAspect.class); User proxy = factory.getProxy(); String userId = "001"; proxy.rent(userId); proxy.back(userId);
輸出結(jié)果:
--開始執(zhí)行租賃動作--
User:租賃【充電寶】
User:歸還【充電寶】
3 配置方式
<!-- 目標類--> <bean id="user" class="net.deniro.spring4.aspectj.User"/> <!-- 切面類--> <bean class="net.deniro.spring4.aspectj.PreRentAspect"/> <!-- 自動創(chuàng)建代理--> <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>
單元測試:
ApplicationContext context = new ClassPathXmlApplicationContext(spring.xml"); User user = (User) context.getBean("user"); String userId = "001"; user.rent(userId); user.back(userId);
輸出結(jié)果與編程方式完全相同。
也可以基于 Schema 的 aop 命名空間進行配置:
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--aspectj 驅(qū)動器 --> <aop:aspectj-autoproxy/> <!-- 目標類--> <bean id="user" class="net.deniro.spring4.aspectj.User"/> <!-- 切面類--> <bean class="net.deniro.spring4.aspectj.PreRentAspect"/> </beans>
這樣的配置更加簡潔。其實在 <aop:aspectj-atuoproxy/>
內(nèi)部已經(jīng)采用了自動代理模式啦 O(∩_∩)O哈哈~
<aop:aspectj-atuoproxy/>
的 proxy-target-class
屬性,默認為 false ,表示使用 JDK 動態(tài)代理技術(shù)織入增強;此值為 true 則表示使用 CGLib 動態(tài)代理技術(shù)織入增強 。 如果目標類沒有聲明接口,那么即使 proxy-target-class
設置為 false,也會自動使用 CGLib 動態(tài)代理織入增強的喲O(∩_∩)O哈哈~
基于 Java5.0+ 的項目,建議使用 AspectJ 來配置切點與增強,因為這樣更簡潔、也更直接。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決MyEclipse10.7部署報錯拋空指針異常問題的方法
這篇文章主要介紹了解決MyEclipse10.7部署報錯拋空指針異常問題的方法,需要的朋友可以參考下2015-12-12淺析Spring容器原始Bean是如何創(chuàng)建的
這篇文章主要是想和小伙伴們一起聊聊?Spring?容器創(chuàng)建?Bean?最最核心的?createBeanInstance?方法,文中的示例代碼講解詳細,需要的可以參考一下2023-08-08java 反射調(diào)用Service導致Spring注入Dao失效的解決方案
這篇文章主要介紹了java 反射調(diào)用Service導致Spring注入Dao失效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Spring Boot 單元測試和集成測試實現(xiàn)詳解
這篇文章主要介紹了Spring Boot 單元測試和集成測試實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09RabbitMQ交換機使用場景和消息可靠性總結(jié)分析
這篇文章主要為大家介紹了RabbitMQ交換機使用場景和消息可靠性總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01每天練一練Java函數(shù)與算法Math函數(shù)總結(jié)與字符串轉(zhuǎn)換整數(shù)
這篇文章主要介紹了Java函數(shù)與算法Math函數(shù)總結(jié)與字符串轉(zhuǎn)換整數(shù),每天練一練,水平在不知不覺中提高,需要的朋友快過來看看吧2021-08-08