Spring使用AspectJ的注解式實現AOP面向切面編程
AOP:面向切面編程,相對于OOP面向對象編程。
Spring的AOP的存在目的是為了解耦。AOP可以讓一組類共享相同的行為。在OOP中只能通過繼承類和實現接口,來使代碼的耦合度增強,而且類的繼承只能為單繼承,阻礙更多行為添加到一組類上,AOP彌補了OOP的不足。
1.2 AOP中的概念 切入點(pointcut): 切入點(pointcut):在哪些類、哪些方法上切入。 通知(advice):在方法前、方法后、方法前后做什么。 切面(aspect):切面 = 切入點 + 通知。即在什么時機、什么地方、做什么。 織入(weaving):把切面加入對象,并創建出代理對象的過程。 環繞通知:AOP中最強大、靈活的通知,它繼承了前置和后置通知,保留了連接點原有的方法。2、認識AspectJ 2.1 AspectJ的簡介AspectJ是一個面向切面編程的框架,它擴展了Java語言。AspectJ定義了AOP語法,它有一個專門的編譯器用來生成遵守Java字節編碼規范的Class文件。AspectJ還支持原生的Java,只需要加上AspectJ提供的注解即可。
2.2 Spring AOP 和 AspectJ比較簡單地說,Spring AOP 和 AspectJ 有不同的目標。
Spring AOP 旨在提供一個跨 Spring IoC 的簡單的 AOP 實現,以解決程序員面臨的最常見問題。它不打算作為一個完整的 AOP 解決方案 —— 它只能應用于由 Spring 容器管理的 Bean。
AspectJ 是原始的 AOP 技術,目的是提供完整的 AOP 解決方案。它更健壯,但也比 Spring AOP 復雜得多。還值得注意的是,AspectJ 可以在所有域對象中應用。
2.3 Spring支持AspectJ的注解式切面編程(1)使用@Aspect聲明一個切面。
(2)使用@After、@Before、@Around定義建言(advice),可直接將攔截規則(切點)作為參數。
(3)其中@After、@Before、@Around參數的攔截規則為切點(PointCut),為了使切點復用,可以使用@Pointcut專門定義攔截規則,然后在@After、@Before、@Around的參數中調用。
(4)其中符合條件的每一個被攔截處為連接點(JoinPoint)。
攔截方式分為:基于注解式攔截、基于方法規則式攔截。
其中注解式攔截能夠很好地控制要攔截的粒度和獲得更豐富的信息,Spring本身在事務處理(@Transactional)和數據緩存(@Cacheable)等都使用了基于注解式攔截。
2.4 AspectJ的注解說明 @Aspect:標記為切面類。 @Before:在切入點開始處切入內容。 @After:在切入點結尾處切入內容。 @AfterReturning:在切入點return內容之后切入內容(可以用來對處理返回值做一些加工處理)。 @Around:在切入點前后切入內容,并自己控制何時執行切入點自身的內容。 @AfterThrowing:用來處理當切入內容部分拋出異常之后的處理邏輯。3、Spring使用AspectJ實現日志記錄操作【實例】使用基于注解式攔截和基于方法規則式攔截兩種方式,實現模擬日志記錄操作。
(1)添加相關的jar包添加SpringAOP支持及AspectJ依賴,pom.xml文件的配置如下:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>5.2.3.RELEASE</spring.version> <aspectj.version>1.9.5</aspectj.version></properties> <dependencies> <!-- Spring框架 --> <dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version> </dependency> <!-- Aspectj依賴 --> <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></dependencies>(2)編寫攔截規則的注解
package com.pjb.aop;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 日志記錄注解 * @author pan_junbiao **/@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface LogAction{ String name();}(3)編寫使用注解的被攔截類
package com.pjb.aop;import org.springframework.stereotype.Service;/** * 使用注解的被攔截類 * @author pan_junbiao **/@Servicepublic class DemoAnnotationService{ @LogAction(name='注解式攔截的add操作') public void add() {System.out.println('執行新增操作'); }}(4)編寫使用方法規則的被攔截類
package com.pjb.aop;import org.springframework.stereotype.Service;/** * 使用方法規則被攔截類 * @author pan_junbiao **/@Servicepublic class DemoMethodService{ public void add() {System.out.println('執行新增操作'); }}(5)編寫切面
package com.pjb.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.*;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import java.lang.reflect.Method;/** * 切面 * @author pan_junbiao * 說明: * 通過@Aspect注解聲明一個切面 * 通過@Component注解讓此切面成為Spring容器管理的Bean **/@Aspect@Componentpublic class LogAspect{ /** * 通過@Pointcut注解聲明切點 */ @Pointcut('@annotation(com.pjb.aop.LogAction)') public void annotationPointCut(){}; /** * 通過@After注解聲明一個建言,并使用@Pointcut注解定義的切點 */ @After('annotationPointCut()') public void after(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature)joinPoint.getSignature();Method method = signature.getMethod();LogAction logAction = method.getAnnotation(LogAction.class);//通過反射獲取注解上的屬性,然后做日志記錄的相關操System.out.println('[日志記錄]注解式攔截,'+logAction.name()); } /** * 通過@Before注解聲明一個建言,此建言直接使用攔截規則作為參數 */ @Before('execution(* com.pjb.aop.DemoMethodService.*(..))') public void before(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature)joinPoint.getSignature();Method method = signature.getMethod();System.out.println('[日志記錄]方法規則式攔截,'+method.getName()); }}(6)配置類
package com.pjb.aop;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;/** * 配置類 * @author pan_junbiao * 說明: * 使用@EnableAspectJAutoProxy注解開啟Spring對AspectJ的支持 **/@Configuration@ComponentScan('com.pjb.aop')@EnableAspectJAutoProxypublic class AopConfig{}(7)運行
package com.pjb.aop;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/** * 測試類 * @author pan_junbiao **/public class AopTest{ public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); demoAnnotationService.add();System.out.println('=======================================');demoMethodService.add(); context.close(); }}
執行結果:
【示例】SpringBoot項目中使用AspectJ實現日志記錄操作。
(1)pom.xml文件的配置<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>(2)編寫AOP日志注解類
package com.pjb.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest; /** * AOP管理日志 * @author pan_junbiao **/@Aspect@Componentpublic class AopLog{ private Logger logger = LoggerFactory.getLogger(this.getClass()); //線程局部的變量,用于解決多線程中相同變量的訪問沖突問題 ThreadLocal<Long> startTime = new ThreadLocal<>(); //定義切點 @Pointcut('execution(public * com.pjb..*.*(..))') public void aopWebLog() { } //使用@Before在切入點開始處切入內容 @Before('aopWebLog()') public void doBefore(JoinPoint joinPoint) throws Throwable {startTime.set(System.currentTimeMillis());// 接收到請求,記錄請求內容ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest(); // 記錄下請求內容logger.info('URL : ' + request.getRequestURL().toString());logger.info('HTTP方法 : ' + request.getMethod());logger.info('IP地址 : ' + request.getRemoteAddr());logger.info('類的方法 : ' + joinPoint.getSignature().getDeclaringTypeName() + '.' + joinPoint.getSignature().getName());//logger.info('參數 : ' + Arrays.toString(joinPoint.getArgs()));logger.info('參數 : ' + request.getQueryString()); } //使用@AfterReturning在切入點return內容之后切入內容(可以用來對處理返回值做一些加工處理) @AfterReturning(pointcut = 'aopWebLog()',returning = 'retObject') public void doAfterReturning(Object retObject) throws Throwable {// 處理完請求,返回內容logger.info('應答值 : ' + retObject);logger.info('費時: ' + (System.currentTimeMillis() - startTime.get())); } //使用@AfterThrowing用來處理當切入內容部分拋出異常之后的處理邏輯 //拋出異常后通知(After throwing advice) : 在方法拋出異常退出時執行的通知。 @AfterThrowing(pointcut = 'aopWebLog()', throwing = 'ex') public void addAfterThrowingLogger(JoinPoint joinPoint, Exception ex) {logger.error('執行 ' + ' 異常', ex); }}(3)編寫控制器用于測試
下面的控制器構造了一個普通的Rest風格的頁面。
package com.pjb.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;/** * 日志控制器 * @author pan_junbiao **/@RestControllerpublic class AopLogController{ @GetMapping('/aoptest') public String AopTest(String userName,String password) {return '您好,歡迎訪問 pan_junbiao的博客'; }}(4)運行
啟動項目,在瀏覽器中訪問 “http://127.0.0.1:8080/aoptest?userName=pan_junbiao&password=123456”
瀏覽器執行結果:
控制臺輸出結果:
網上大多數介紹AspectJ的文章都是和Spring容器混用的,但有時我們想自己寫框架就需要拋開Spring造輪子,類似使用原生AspectJ達到面向切面編程。步驟很簡單,只需要兩步。
1.導入依賴<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.3</version></dependency>2.Maven插件
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.10</version> <configuration><source>1.8</source><target>1.8</target><complianceLevel>1.8</complianceLevel> </configuration> <executions><execution> <goals><goal>compile</goal> </goals></execution> </executions></plugin>3.使用注解
@Aspectpublic class AspectDemo { @Pointcut('execution(* cn.yueshutong.App.say())') private void pointcut() {} // signature @Before('pointcut()') public void before(){System.out.println('Hello'); }}
App.java
public class App { public static void main( String[] args ) {System.out.println( new App().say() ); } public String say() {return 'World'; }}
這一步就和平常使用Spring AOP注解沒有什么區別了。
4.織入/代理我們都知道,Spring AOP是通過動態代理生成一個代理類,這種方式的最大缺點就是對于對象內部的方法嵌套調用不會走代理類,比如下面這段代碼:
@Componentpublic class TestComponent { @TestAspect public void work(){//do sth } public void call(){work(); }}
原因很簡單,對象內部的方法調用該對象的其他方法是通過自身this進行引用,并不是通過代理類引用。而AspectJ則不同,AspectJ是通過織入的方式將切面代碼織入進原對象內部,并不會生成額外的代理類。
關于這一點,我們反編譯看一下切點代碼:
//原方法 public void say() {System.out.println(this.getClass().getName());hi(); } //反編譯 public void say() {ResourceAspect.aspectOf().before();System.out.println(this.getClass().getName());this.hi(); }
深究下去,在Spring AOP中,我們只有調用代理類的切點方法才能觸發Before方法,因為代理類本質上是對原類的一層封裝,原類是沒有變化的,原類的方法內部的this指向的依舊是原類,這就導致了原類方法內部的嵌套調用無法被代理類感知到,而AspectJ的織入就不同了,它會動態改變你的原類代碼,將Before等方法全部寫入進你的原方法中,這就保證了面向切面編程的萬無一失。
兩種方式,各有利弊,如何使用還需要視情況而行。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
