Spring使用AspectJ的注解式實(shí)現(xiàn)AOP面向切面編程
1、認(rèn)識(shí)Spring AOP
1.1 AOP的簡(jiǎn)介
AOP:面向切面編程,相對(duì)于OOP面向?qū)ο缶幊獭?/p>
Spring的AOP的存在目的是為了解耦。AOP可以讓一組類(lèi)共享相同的行為。在OOP中只能通過(guò)繼承類(lèi)和實(shí)現(xiàn)接口,來(lái)使代碼的耦合度增強(qiáng),而且類(lèi)的繼承只能為單繼承,阻礙更多行為添加到一組類(lèi)上,AOP彌補(bǔ)了OOP的不足。
1.2 AOP中的概念 切入點(diǎn)(pointcut):
- 切入點(diǎn)(pointcut):在哪些類(lèi)、哪些方法上切入。
- 通知(advice):在方法前、方法后、方法前后做什么。
- 切面(aspect):切面 = 切入點(diǎn) + 通知。即在什么時(shí)機(jī)、什么地方、做什么。
- 織入(weaving):把切面加入對(duì)象,并創(chuàng)建出代理對(duì)象的過(guò)程。
- 環(huán)繞通知:AOP中最強(qiáng)大、靈活的通知,它繼承了前置和后置通知,保留了連接點(diǎn)原有的方法。
2、認(rèn)識(shí)AspectJ 2.1 AspectJ的簡(jiǎn)介
AspectJ是一個(gè)面向切面編程的框架,它擴(kuò)展了Java語(yǔ)言。AspectJ定義了AOP語(yǔ)法,它有一個(gè)專(zhuān)門(mén)的編譯器用來(lái)生成遵守Java字節(jié)編碼規(guī)范的Class文件。AspectJ還支持原生的Java,只需要加上AspectJ提供的注解即可。
2.2 Spring AOP 和 AspectJ比較
簡(jiǎn)單地說(shuō),Spring AOP 和 AspectJ 有不同的目標(biāo)。
Spring AOP 旨在提供一個(gè)跨 Spring IoC 的簡(jiǎn)單的 AOP 實(shí)現(xiàn),以解決程序員面臨的最常見(jiàn)問(wèn)題。它不打算作為一個(gè)完整的 AOP 解決方案 —— 它只能應(yīng)用于由 Spring 容器管理的 Bean。
AspectJ 是原始的 AOP 技術(shù),目的是提供完整的 AOP 解決方案。它更健壯,但也比 Spring AOP 復(fù)雜得多。還值得注意的是,AspectJ 可以在所有域?qū)ο笾袘?yīng)用。
2.3 Spring支持AspectJ的注解式切面編程
(1)使用@Aspect聲明一個(gè)切面。
(2)使用@After、@Before、@Around定義建言(advice),可直接將攔截規(guī)則(切點(diǎn))作為參數(shù)。
(3)其中@After、@Before、@Around參數(shù)的攔截規(guī)則為切點(diǎn)(PointCut),為了使切點(diǎn)復(fù)用,可以使用@Pointcut專(zhuān)門(mén)定義攔截規(guī)則,然后在@After、@Before、@Around的參數(shù)中調(diào)用。
(4)其中符合條件的每一個(gè)被攔截處為連接點(diǎn)(JoinPoint)。
攔截方式分為:基于注解式攔截、基于方法規(guī)則式攔截。
其中注解式攔截能夠很好地控制要攔截的粒度和獲得更豐富的信息,Spring本身在事務(wù)處理(@Transactional)和數(shù)據(jù)緩存(@Cacheable)等都使用了基于注解式攔截。
2.4 AspectJ的注解說(shuō)明
- @Aspect:標(biāo)記為切面類(lèi)。
- @Before:在切入點(diǎn)開(kāi)始處切入內(nèi)容。
- @After:在切入點(diǎn)結(jié)尾處切入內(nèi)容。
- @AfterReturning:在切入點(diǎn)return內(nèi)容之后切入內(nèi)容(可以用來(lái)對(duì)處理返回值做一些加工處理)。
- @Around:在切入點(diǎn)前后切入內(nèi)容,并自己控制何時(shí)執(zhí)行切入點(diǎn)自身的內(nèi)容。
- @AfterThrowing:用來(lái)處理當(dāng)切入內(nèi)容部分拋出異常之后的處理邏輯。
3、Spring使用AspectJ實(shí)現(xiàn)日志記錄操作
【實(shí)例】使用基于注解式攔截和基于方法規(guī)則式攔截兩種方式,實(shí)現(xiàn)模擬日志記錄操作。
(1)添加相關(guān)的jar包
添加SpringAOP支持及AspectJ依賴(lài),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依賴(lài) -->
<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)編寫(xiě)攔截規(guī)則的注解
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)編寫(xiě)使用注解的被攔截類(lèi)
package com.pjb.aop;
import org.springframework.stereotype.Service;
/**
* 使用注解的被攔截類(lèi)
* @author pan_junbiao
**/
@Service
public class DemoAnnotationService
{
@LogAction(name="注解式攔截的add操作")
public void add()
{
System.out.println("執(zhí)行新增操作");
}
}
(4)編寫(xiě)使用方法規(guī)則的被攔截類(lèi)
package com.pjb.aop;
import org.springframework.stereotype.Service;
/**
* 使用方法規(guī)則被攔截類(lèi)
* @author pan_junbiao
**/
@Service
public class DemoMethodService
{
public void add()
{
System.out.println("執(zhí)行新增操作");
}
}
(5)編寫(xiě)切面
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
* 說(shuō)明:
* 通過(guò)@Aspect注解聲明一個(gè)切面
* 通過(guò)@Component注解讓此切面成為Spring容器管理的Bean
**/
@Aspect
@Component
public class LogAspect
{
/**
* 通過(guò)@Pointcut注解聲明切點(diǎn)
*/
@Pointcut("@annotation(com.pjb.aop.LogAction)")
public void annotationPointCut(){};
/**
* 通過(guò)@After注解聲明一個(gè)建言,并使用@Pointcut注解定義的切點(diǎn)
*/
@After("annotationPointCut()")
public void after(JoinPoint joinPoint)
{
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
LogAction logAction = method.getAnnotation(LogAction.class);
//通過(guò)反射獲取注解上的屬性,然后做日志記錄的相關(guān)操
System.out.println("[日志記錄](méi)注解式攔截,"+logAction.name());
}
/**
* 通過(guò)@Before注解聲明一個(gè)建言,此建言直接使用攔截規(guī)則作為參數(shù)
*/
@Before("execution(* com.pjb.aop.DemoMethodService.*(..))")
public void before(JoinPoint joinPoint)
{
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("[日志記錄](méi)方法規(guī)則式攔截,"+method.getName());
}
}
(6)配置類(lèi)
package com.pjb.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* 配置類(lèi)
* @author pan_junbiao
* 說(shuō)明:
* 使用@EnableAspectJAutoProxy注解開(kāi)啟Spring對(duì)AspectJ的支持
**/
@Configuration
@ComponentScan("com.pjb.aop")
@EnableAspectJAutoProxy
public class AopConfig
{
}
(7)運(yùn)行
package com.pjb.aop;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* 測(cè)試類(lèi)
* @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();
}
}
執(zhí)行結(jié)果:

4、SpringBoot使用AspectJ實(shí)現(xiàn)日志記錄操作
【示例】SpringBoot項(xiàng)目中使用AspectJ實(shí)現(xiàn)日志記錄操作。
(1)pom.xml文件的配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
(2)編寫(xiě)AOP日志注解類(lèi)
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
@Component
public class AopLog
{
private Logger logger = LoggerFactory.getLogger(this.getClass());
//線程局部的變量,用于解決多線程中相同變量的訪問(wèn)沖突問(wèn)題
ThreadLocal<Long> startTime = new ThreadLocal<>();
//定義切點(diǎn)
@Pointcut("execution(public * com.pjb..*.*(..))")
public void aopWebLog() {
}
//使用@Before在切入點(diǎn)開(kāi)始處切入內(nèi)容
@Before("aopWebLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
startTime.set(System.currentTimeMillis());
// 接收到請(qǐng)求,記錄請(qǐng)求內(nèi)容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 記錄下請(qǐng)求內(nèi)容
logger.info("URL : " + request.getRequestURL().toString());
logger.info("HTTP方法 : " + request.getMethod());
logger.info("IP地址 : " + request.getRemoteAddr());
logger.info("類(lèi)的方法 : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
//logger.info("參數(shù) : " + Arrays.toString(joinPoint.getArgs()));
logger.info("參數(shù) : " + request.getQueryString());
}
//使用@AfterReturning在切入點(diǎn)return內(nèi)容之后切入內(nèi)容(可以用來(lái)對(duì)處理返回值做一些加工處理)
@AfterReturning(pointcut = "aopWebLog()",returning = "retObject")
public void doAfterReturning(Object retObject) throws Throwable {
// 處理完請(qǐng)求,返回內(nèi)容
logger.info("應(yīng)答值 : " + retObject);
logger.info("費(fèi)時(shí): " + (System.currentTimeMillis() - startTime.get()));
}
//使用@AfterThrowing用來(lái)處理當(dāng)切入內(nèi)容部分拋出異常之后的處理邏輯
//拋出異常后通知(After throwing advice) : 在方法拋出異常退出時(shí)執(zhí)行的通知。
@AfterThrowing(pointcut = "aopWebLog()", throwing = "ex")
public void addAfterThrowingLogger(JoinPoint joinPoint, Exception ex) {
logger.error("執(zhí)行 " + " 異常", ex);
}
}
(3)編寫(xiě)控制器用于測(cè)試
下面的控制器構(gòu)造了一個(gè)普通的Rest風(fēng)格的頁(yè)面。
package com.pjb.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 日志控制器
* @author pan_junbiao
**/
@RestController
public class AopLogController
{
@GetMapping("/aoptest")
public String AopTest(String userName,String password)
{
return "您好,歡迎訪問(wèn) pan_junbiao的博客";
}
}
(4)運(yùn)行
啟動(dòng)項(xiàng)目,在瀏覽器中訪問(wèn) “http://127.0.0.1:8080/aoptest?userName=pan_junbiao&password=123456”
瀏覽器執(zhí)行結(jié)果:

控制臺(tái)輸出結(jié)果:

不依賴(lài)Spring使用AspectJ達(dá)到AOP面向切面編程
網(wǎng)上大多數(shù)介紹AspectJ的文章都是和Spring容器混用的,但有時(shí)我們想自己寫(xiě)框架就需要拋開(kāi)Spring造輪子,類(lèi)似使用原生AspectJ達(dá)到面向切面編程。步驟很簡(jiǎn)單,只需要兩步。
1.導(dǎo)入依賴(lài)
<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.使用注解
@Aspect
public 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注解沒(méi)有什么區(qū)別了。
4.織入/代理
我們都知道,Spring AOP是通過(guò)動(dòng)態(tài)代理生成一個(gè)代理類(lèi),這種方式的最大缺點(diǎn)就是對(duì)于對(duì)象內(nèi)部的方法嵌套調(diào)用不會(huì)走代理類(lèi),比如下面這段代碼:
@Component
public class TestComponent {
@TestAspect
public void work(){
//do sth
}
public void call(){
work();
}
}
原因很簡(jiǎn)單,對(duì)象內(nèi)部的方法調(diào)用該對(duì)象的其他方法是通過(guò)自身this進(jìn)行引用,并不是通過(guò)代理類(lèi)引用。而AspectJ則不同,AspectJ是通過(guò)織入的方式將切面代碼織入進(jìn)原對(duì)象內(nèi)部,并不會(huì)生成額外的代理類(lèi)。
關(guān)于這一點(diǎn),我們反編譯看一下切點(diǎn)代碼:
//原方法
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中,我們只有調(diào)用代理類(lèi)的切點(diǎn)方法才能觸發(fā)Before方法,因?yàn)榇眍?lèi)本質(zhì)上是對(duì)原類(lèi)的一層封裝,原類(lèi)是沒(méi)有變化的,原類(lèi)的方法內(nèi)部的this指向的依舊是原類(lèi),這就導(dǎo)致了原類(lèi)方法內(nèi)部的嵌套調(diào)用無(wú)法被代理類(lèi)感知到,而AspectJ的織入就不同了,它會(huì)動(dòng)態(tài)改變你的原類(lèi)代碼,將Before等方法全部寫(xiě)入進(jìn)你的原方法中,這就保證了面向切面編程的萬(wàn)無(wú)一失。
兩種方式,各有利弊,如何使用還需要視情況而行。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis圖文并茂講解注解開(kāi)發(fā)一對(duì)多查詢(xún)
這篇文章主要介紹了SpringBoot中Mybatis注解一對(duì)多查詢(xún)的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Java基于drools做規(guī)則校驗(yàn)的實(shí)現(xiàn)
工作中需要開(kāi)發(fā)一個(gè)規(guī)則服務(wù),提供各種規(guī)則,本文主要介紹了Java基于drools做規(guī)則校驗(yàn)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
設(shè)置JavaScript自動(dòng)提示-Eclipse/MyEclipse
自動(dòng)提示需要2個(gè)組件,分別是:ext-4.0.2a.jsb2||spket-1.6.16.jar,需要的朋友可以參考下2016-05-05
Java實(shí)現(xiàn)超大Excel文件解析(XSSF,SXSSF,easyExcel)
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)超大Excel文件解析(XSSF,SXSSF,easyExcel)以及速度的對(duì)比,感興趣的可以了解一下2022-07-07
java Hibernate 一對(duì)多自身關(guān)聯(lián)問(wèn)題
formBean在提交表單的時(shí)候,域中數(shù)據(jù)庫(kù)在下一次中仍然保留引起的,struts formBean 默認(rèn)的scope為session,手動(dòng)設(shè)置為request,就好了2008-07-07
java實(shí)現(xiàn)學(xué)生成績(jī)信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)學(xué)生成績(jī)信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07

