Spring使用AspectJ開發AOP:基於Annotation


基於 Annotation 的聲明式

在 Spring 中,盡管使用 XML 配置文件可以實現 AOP 開發,但是如果所有的相關的配置都集中在配置文件中,勢必會導致 XML 配置文件過於臃腫,從而給維護和升級帶來一定的困難。

為此,AspectJ 框架為 AOP 開發提供了另一種開發方式——基於 Annotation 的聲明式。AspectJ 允許使用注解定義切面、切入點和增強處理,而 Spring 框架則可以識別並根據這些注解生成 AOP 代理。

關於 Annotation 注解的介紹如表 1 所示。

表 1 Annotation 注解介紹
名稱 說明
@Aspect 用於定義一個切面。
@Before 用於定義前置通知,相當於 BeforeAdvice。
@AfterReturning 用於定義后置通知,相當於 AfterReturningAdvice。
@Around 用於定義環繞通知,相當於MethodInterceptor。
@AfterThrowing 用於定義拋出通知,相當於ThrowAdvice。
@After 用於定義最終final通知,不管是否異常,該通知都會執行。
@DeclareParents 用於定義引介通知,相當於IntroductionInterceptor(不要求掌握)。


下面使用注解的方式重新實現《基於XML的聲明式》部分的功能。

1. 創建切面類 MyAspect

在 src 目錄下創建一個名為 com.mengma.aspectj.annotation 的包,在該包下創建一個切面類 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,沒有值,名稱自定義,沒有參數
@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());
}

// 環繞通知
@Around("myPointCut()")
public Object myAround(ProceedingJoinPoint proceedingJoinPoint)
throws Throwable {
System.out.println("環繞開始"); // 開始
Object obj = proceedingJoinPoint.proceed(); // 執行當前目標方法
System.out.println("環繞結束"); // 結束
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”作為參數傳遞給要執行的方法,如需其他參數(如異常通知的異常參數),可以根據代碼提示傳遞相應的屬性值。

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("正式業務");
    }
}

 

3. 創建Spring配置文件

在 com.mengma.aspectj.annotation 包下創建 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. 創建測試類

在 com.mengma.aspectj.annotation 包下創建一個名為 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");
// 執行方法
customerDao.add();
}
}

 

5. 運行項目並查看結果

使用 JUnit 測試運行 test() 方法,運行成功后,控制台的輸出結果如圖 3 所示。

運行結果
圖 3  運行結果


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

運行結果
圖 4  運行結果


從圖 3 和圖 4 的輸出結果中可以看出,已成功使用 Annotation 的方式實現了 AOP 開發。與其他方式相比,基於 Annotation 方式實現 AOP 的效果是最方便的方式,所以實際開發中推薦使用注解的方式。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM