溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解在Spring中如何使用AspectJ來實現(xiàn)AOP

發(fā)布時間:2020-09-27 09:00:51 來源:腳本之家 閱讀:140 作者:deniro 欄目:編程語言

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();

//設(shè)置目標類
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 設(shè)置為 false,也會自動使用 CGLib 動態(tài)代理織入增強的喲O(∩_∩)O哈哈~

基于 Java5.0+ 的項目,建議使用 AspectJ 來配置切點與增強,因為這樣更簡潔、也更直接。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI