溫馨提示×

溫馨提示×

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

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

Spring如何利用AspectJ的注解式實現(xiàn)AOP面向切面編程

發(fā)布時間:2021-06-15 14:26:44 來源:億速云 閱讀:261 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Spring如何利用AspectJ的注解式實現(xiàn)AOP面向切面編程”,在日常操作中,相信很多人在Spring如何利用AspectJ的注解式實現(xiàn)AOP面向切面編程問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Spring如何利用AspectJ的注解式實現(xiàn)AOP面向切面編程”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

1、認(rèn)識Spring AOP

1.1 AOP的簡介

AOP:面向切面編程,相對于OOP面向?qū)ο缶幊獭?/p>

Spring的AOP的存在目的是為了解耦。AOP可以讓一組類共享相同的行為。在OOP中只能通過繼承類和實現(xiàn)接口,來使代碼的耦合度增強,而且類的繼承只能為單繼承,阻礙更多行為添加到一組類上,AOP彌補了OOP的不足。

1.2 AOP中的概念 切入點(pointcut):

  • 切入點(pointcut):在哪些類、哪些方法上切入。

  • 通知(advice):在方法前、方法后、方法前后做什么。

  • 切面(aspect):切面 = 切入點 + 通知。即在什么時機、什么地方、做什么。

  • 織入(weaving):把切面加入對象,并創(chuàng)建出代理對象的過程。

  • 環(huán)繞通知:AOP中最強大、靈活的通知,它繼承了前置和后置通知,保留了連接點原有的方法。

2、認(rèn)識AspectJ 2.1 AspectJ的簡介

AspectJ是一個面向切面編程的框架,它擴展了Java語言。AspectJ定義了AOP語法,它有一個專門的編譯器用來生成遵守Java字節(jié)編碼規(guī)范的Class文件。AspectJ還支持原生的Java,只需要加上AspectJ提供的注解即可。

2.2 Spring AOP 和 AspectJ比較

簡單地說,Spring AOP 和 AspectJ 有不同的目標(biāo)。

Spring AOP 旨在提供一個跨 Spring IoC 的簡單的 AOP 實現(xiàn),以解決程序員面臨的最常見問題。它不打算作為一個完整的 AOP 解決方案 —— 它只能應(yīng)用于由 Spring 容器管理的 Bean。

AspectJ 是原始的 AOP 技術(shù),目的是提供完整的 AOP 解決方案。它更健壯,但也比 Spring AOP 復(fù)雜得多。還值得注意的是,AspectJ 可以在所有域?qū)ο笾袘?yīng)用。

2.3 Spring支持AspectJ的注解式切面編程

(1)使用@Aspect聲明一個切面。

(2)使用@After、@Before、@Around定義建言(advice),可直接將攔截規(guī)則(切點)作為參數(shù)。

(3)其中@After、@Before、@Around參數(shù)的攔截規(guī)則為切點(PointCut),為了使切點復(fù)用,可以使用@Pointcut專門定義攔截規(guī)則,然后在@After、@Before、@Around的參數(shù)中調(diào)用。

(4)其中符合條件的每一個被攔截處為連接點(JoinPoint)。

攔截方式分為:基于注解式攔截、基于方法規(guī)則式攔截。

其中注解式攔截能夠很好地控制要攔截的粒度和獲得更豐富的信息,Spring本身在事務(wù)處理(@Transactional)和數(shù)據(jù)緩存(@Cacheable)等都使用了基于注解式攔截。

2.4 AspectJ的注解說明

  • @Aspect:標(biāo)記為切面類。

  • @Before:在切入點開始處切入內(nèi)容。

  • @After:在切入點結(jié)尾處切入內(nèi)容。

  • @AfterReturning:在切入點return內(nèi)容之后切入內(nèi)容(可以用來對處理返回值做一些加工處理)。

  • @Around:在切入點前后切入內(nèi)容,并自己控制何時執(zhí)行切入點自身的內(nèi)容。

  • @AfterThrowing:用來處理當(dāng)切入內(nèi)容部分拋出異常之后的處理邏輯。

3、Spring使用AspectJ實現(xiàn)日志記錄操作

【實例】使用基于注解式攔截和基于方法規(guī)則式攔截兩種方式,實現(xiàn)模擬日志記錄操作。

(1)添加相關(guān)的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)編寫攔截規(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)編寫使用注解的被攔截類

package com.pjb.aop;
import org.springframework.stereotype.Service;
/**
 * 使用注解的被攔截類
 * @author pan_junbiao
 **/
@Service
public class DemoAnnotationService
{
    @LogAction(name="注解式攔截的add操作")
    public void add()
    {
        System.out.println("執(zhí)行新增操作");
    }
}

(4)編寫使用方法規(guī)則的被攔截類

package com.pjb.aop;
import org.springframework.stereotype.Service;
/**
 * 使用方法規(guī)則被攔截類
 * @author pan_junbiao
 **/
@Service
public class DemoMethodService
{
    public void add()
    {
        System.out.println("執(zhí)行新增操作");
    }
}

(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
@Component
public 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);
        //通過反射獲取注解上的屬性,然后做日志記錄的相關(guān)操
        System.out.println("[日志記錄]注解式攔截,"+logAction.name());
    }
 
    /**
     * 通過@Before注解聲明一個建言,此建言直接使用攔截規(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("[日志記錄]方法規(guī)則式攔截,"+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")
@EnableAspectJAutoProxy
public 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();
    }
}

執(zhí)行結(jié)果:

Spring如何利用AspectJ的注解式實現(xiàn)AOP面向切面編程

4、SpringBoot使用AspectJ實現(xiàn)日志記錄操作

【示例】SpringBoot項目中使用AspectJ實現(xiàn)日志記錄操作。

(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
@Component
public class AopLog
{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
 
    //線程局部的變量,用于解決多線程中相同變量的訪問沖突問題
    ThreadLocal<Long> startTime = new ThreadLocal<>();
 
    //定義切點
    @Pointcut("execution(public * com.pjb..*.*(..))")
    public void aopWebLog() {
    }
 
    //使用@Before在切入點開始處切入內(nèi)容
    @Before("aopWebLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        startTime.set(System.currentTimeMillis());
        // 接收到請求,記錄請求內(nèi)容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
 
        // 記錄下請求內(nèi)容
        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("參數(shù) : " + Arrays.toString(joinPoint.getArgs()));
        logger.info("參數(shù) : " + request.getQueryString());
    }
 
    //使用@AfterReturning在切入點return內(nèi)容之后切入內(nèi)容(可以用來對處理返回值做一些加工處理)
    @AfterReturning(pointcut = "aopWebLog()",returning = "retObject")
    public void doAfterReturning(Object retObject) throws Throwable {
        // 處理完請求,返回內(nèi)容
        logger.info("應(yīng)答值 : " + retObject);
        logger.info("費時: " + (System.currentTimeMillis() - startTime.get()));
    }
 
    //使用@AfterThrowing用來處理當(dāng)切入內(nèi)容部分拋出異常之后的處理邏輯
    //拋出異常后通知(After throwing advice) : 在方法拋出異常退出時執(zhí)行的通知。
    @AfterThrowing(pointcut = "aopWebLog()", throwing = "ex")
    public void addAfterThrowingLogger(JoinPoint joinPoint, Exception ex) {
        logger.error("執(zhí)行 " + " 異常", ex);
    }
}

(3)編寫控制器用于測試

下面的控制器構(gòu)造了一個普通的Rest風(fēng)格的頁面。

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 "您好,歡迎訪問 pan_junbiao的博客";
    }
}

(4)運行

啟動項目,在瀏覽器中訪問 “http://127.0.0.1:8080/aoptest?userName=pan_junbiao&password=123456”

瀏覽器執(zhí)行結(jié)果:

Spring如何利用AspectJ的注解式實現(xiàn)AOP面向切面編程

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

Spring如何利用AspectJ的注解式實現(xiàn)AOP面向切面編程

不依賴Spring使用AspectJ達到AOP面向切面編程

網(wǎng)上大多數(shù)介紹AspectJ的文章都是和Spring容器混用的,但有時我們想自己寫框架就需要拋開Spring造輪子,類似使用原生AspectJ達到面向切面編程。步驟很簡單,只需要兩步。

1.導(dǎo)入依賴

<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注解沒有什么區(qū)別了。

4.織入/代理

我們都知道,Spring AOP是通過動態(tài)代理生成一個代理類,這種方式的最大缺點就是對于對象內(nèi)部的方法嵌套調(diào)用不會走代理類,比如下面這段代碼:

@Component
public class TestComponent {
    @TestAspect
    public void work(){
        //do sth
    }
 
    public void call(){
        work();
    }
}

原因很簡單,對象內(nèi)部的方法調(diào)用該對象的其他方法是通過自身this進行引用,并不是通過代理類引用。而AspectJ則不同,AspectJ是通過織入的方式將切面代碼織入進原對象內(nèi)部,并不會生成額外的代理類。

關(guā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)用代理類的切點方法才能觸發(fā)Before方法,因為代理類本質(zhì)上是對原類的一層封裝,原類是沒有變化的,原類的方法內(nèi)部的this指向的依舊是原類,這就導(dǎo)致了原類方法內(nèi)部的嵌套調(diào)用無法被代理類感知到,而AspectJ的織入就不同了,它會動態(tài)改變你的原類代碼,將Before等方法全部寫入進你的原方法中,這就保證了面向切面編程的萬無一失。

兩種方式,各有利弊,如何使用還需要視情況而行。

到此,關(guān)于“Spring如何利用AspectJ的注解式實現(xiàn)AOP面向切面編程”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(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