Spring用AspectJ開發(fā)AOP(基于Annotation)
基于 Annotation 的聲明式
在 Spring 中,盡管使用 XML 配置文件可以實現(xiàn) AOP 開發(fā),但是如果所有的相關(guān)的配置都集中在配置文件中,勢必會導致 XML 配置文件過于臃腫,從而給維護和升級帶來一定的困難。
為此,AspectJ 框架為 AOP 開發(fā)提供了另一種開發(fā)方式——基于 Annotation 的聲明式。AspectJ 允許使用注解定義切面、切入點和增強處理,而 Spring 框架則可以識別并根據(jù)這些注解生成 AOP 代理。
關(guān)于 Annotation 注解的介紹如表 1 所示。
表 1 Annotation 注解介紹
| 名稱 | 說明 |
|---|---|
| @Aspect | 用于定義一個切面。 |
| @Before | 用于定義前置通知,相當于 BeforeAdvice。 |
| @AfterReturning | 用于定義后置通知,相當于 AfterReturningAdvice。 |
| @Around | 用于定義環(huán)繞通知,相當于MethodInterceptor。 |
| @AfterThrowing | 用于定義拋出通知,相當于ThrowAdvice。 |
| @After | 用于定義最終final通知,不管是否異常,該通知都會執(zhí)行。 |
| @DeclareParents | 用于定義引介通知,相當于IntroductionInterceptor(不要求掌握)。 |
下面使用注解的方式重新實現(xiàn)《基于XML的聲明式》部分的功能。
1. 創(chuàng)建切面類 MyAspect
在 src 目錄下創(chuàng)建一個名為 com.mengma.aspectj.annotation 的包,在該包下創(chuàng)建一個切面類 MyAspect,如下所示。
package com.mengma.aspectj.annotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
//切面類
@Aspect
@Component
public class MyAspect {
// 用于取代:<aop:pointcut
// expression="execution(*com.mengma.dao..*.*(..))" id="myPointCut"/>
// 要求:方法必須是private,沒有值,名稱自定義,沒有參數(shù)
@Pointcut("execution(*com.mengma.dao..*.*(..))")
private void myPointCut() {
}
// 前置通知
@Before("myPointCut()")
public void myBefore(JoinPoint joinPoint) {
System.out.print("前置通知,目標:");
System.out.print(joinPoint.getTarget() + "方法名稱:");
System.out.println(joinPoint.getSignature().getName());
}
// 后置通知
@AfterReturning(value = "myPointCut()")
public void myAfterReturning(JoinPoint joinPoint) {
System.out.print("后置通知,方法名稱:" + joinPoint.getSignature().getName());
}
// 環(huán)繞通知
@Around("myPointCut()")
public Object myAround(ProceedingJoinPoint proceedingJoinPoint)
throws Throwable {
System.out.println("環(huán)繞開始"); // 開始
Object obj = proceedingJoinPoint.proceed(); // 執(zhí)行當前目標方法
System.out.println("環(huán)繞結(jié)束"); // 結(jié)束
return obj;
}
// 異常通知
@AfterThrowing(value = "myPointCut()", throwing = "e")
public void myAfterThrowing(JoinPoint joinPoint, Throwable e) {
System.out.println("異常通知" + "出錯了" + e.getMessage());
}
// 最終通知
@After("myPointCut()")
public void myAfter() {
System.out.println("最終通知");
}
}
上述代碼中,第 13 行 @Aspect 注解用于聲明這是一個切面類,該類作為組件使用,所以要添加 @Component 注解才能生效。第 19 行中 @Poincut 注解用于配置切入點,取代 XML 文件中配置切入點的代碼。
在每個通知相應的方法上都添加了注解聲明,并且將切入點方法名“myPointCut”作為參數(shù)傳遞給要執(zhí)行的方法,如需其他參數(shù)(如異常通知的異常參數(shù)),可以根據(jù)代碼提示傳遞相應的屬性值。
2. 為目標類添加注解
在 com.mengma.dao.CustomerDaoImpl 目標類中添加注解 @Repository("customerDao")。
import org.springframework.stereotype.Repository;
@Repository("customerDao")
public class CustomerDao {
public void doSome(){
// int i=1/0;
System.out.println("正式業(yè)務");
}
}
3. 創(chuàng)建Spring配置文件
在 com.mengma.aspectj.annotation 包下創(chuàng)建 applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--掃描含com.mengma包下的所有注解--> <context:component-scan base-package="com.mengma"/> <!-- 使切面開啟自動代理 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
上述代碼中,首先導入了 AOP 命名空間及其配套的約束,使切面類中的 @AspectJ 注解能夠正常工作;第 13 行代碼添加了掃描包,使注解生效。需要注意的是,這里還包括目標類 com.mengma.dao.CustomerDaoImpl 的注解,所以 base-package 的值為 com.mengma;第 15 行代碼的作用是切面開啟自動代理。
4. 創(chuàng)建測試類
在 com.mengma.aspectj.annotation 包下創(chuàng)建一個名為 AnnotationTest 的測試類,如下所示。
package com.mengma.aspectj.annotation;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mengma.dao.CustomerDao;
public class AnnotationTest {
@Test
public void test() {
String xmlPath = "com/mengma/aspectj/xml/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
xmlPath);
// 從spring容器獲取實例
CustomerDao customerDao = (CustomerDao) applicationContext
.getBean("customerDao");
// 執(zhí)行方法
customerDao.add();
}
}
5. 運行項目并查看結(jié)果
使用 JUnit 測試運行 test() 方法,運行成功后,控制臺的輸出結(jié)果如圖 3 所示。

圖 3 運行結(jié)果
刪除 add() 方法中的“int i=1/0;”,重新運行 test() 方法,此時控制臺的輸出結(jié)果如圖 4 所示。

圖 4 運行結(jié)果
從圖 3 和圖 4 的輸出結(jié)果中可以看出,已成功使用 Annotation 的方式實現(xiàn)了 AOP 開發(fā)。與其他方式相比,基于 Annotation 方式實現(xiàn) AOP 的效果是最方便的方式,所以實際開發(fā)中推薦使用注解的方式。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot 事件監(jiān)聽的實現(xiàn)方法
這篇文章主要介紹了springboot 事件監(jiān)聽的實現(xiàn)方法,并詳細的介紹了四種監(jiān)聽方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
java用字節(jié)數(shù)組解決FileInputStream讀取漢字出現(xiàn)亂碼問題
這篇文章主要介紹了java用字節(jié)數(shù)組解決FileInputStream讀取漢字出現(xiàn)亂碼問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot實現(xiàn)熱部署Community的示例代碼
本文主要介紹了SpringBoot實現(xiàn)熱部署Community的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06

