Spring-實現AOP的三種方式演示

對於AOP,我們明確的知道,是Spring的核心之一,這裏,博主分享Spring實現AOP的三種方式。

準備環境如下:

  • 導入對應依賴:
<!--Spring的web包依賴其他Spring的包,Maven會自動導入-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>

<!--切面織入包-->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.5</version>
</dependency>

<!--單元測試包-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
  • 準備Spring的配置文件,applicationContext.xml,一定要引入AOP的約束
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

</beans>

準備一個模擬業務層service,其中包括UserService接口和UserServiceImpl實現類:
UserService接口:

package com.ara.service;

//模擬增刪查改的業務層
public interface UserService {

    void add();

    void delete();

    void update();

    void query();

}

UserServiceImpl實現類:

package com.ara.service;

public class UserServiceImpl implements UserService {
    public void add() {
        System.out.println("增加了一個用戶");
    }

    public void delete() {
        System.out.println("刪除了一個用戶");
    }

    public void update() {
        System.out.println("修改了一個用戶");
    }

    public void query() {
        System.out.println("查詢了一個用戶");
    }
}

將業務實現的bean添加到Spring配置中:

<bean id="userService" class="com.ara.service.UserServiceImpl"/>

測試代碼:

@Test
public void springTest(){

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    UserService userService = context.getBean("userService", UserService.class);

    userService.add();

}

測試代碼一直保持不變,就更能體現出AOP的思想了。
這樣直接調用的結果爲:
在這裏插入圖片描述
現在的需求是:在不改動上述代碼的前提下,我們調用其中的方法會打印出對應的切入信息。

1. 方式一:使用原生Spring API接口

編寫切入類:

BeforeLog:

package com.ara.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

//方法前置通知 實現Spring框架的方法前置通知接口
public class BeforeLog implements MethodBeforeAdvice {


    /**
     * @param method 要執行的目標方法的對象
     * @param args   參數列表
     * @param target 目標對象
     * @throws Throwable
     */
    public void before(Method method, Object[] args, Object target) throws Throwable {

        System.out.println("前置切入");
        System.out.println(target.getClass().getName() + "-" + method.getName());

    }
}

AfterLog:

package com.ara.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

//方法前置通知 實現Spring框架的方法後置通知接口
public class AfterLog implements AfterReturningAdvice {
    /**
     * @param returnValue 返回值
     * @param method      執行了什麼方法
     * @param args        執行方法的參數
     * @param target      執行的目標
     * @throws Throwable
     */
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {

        System.out.println("後置切入");
        System.out.println(method.getName() + ":" + returnValue);

    }
}

然後我們在Spring配置文件中配置切面:

<!--  方式一:使用原生Spring API接口  -->
<!--  添加兩個日誌bean  -->
<bean id="beforeLog" class="com.ara.log.BeforeLog"/>
<bean id="afterLog" class="com.ara.log.AfterLog"/>
<!--  配置aop:需要導入aop的約束  -->
<aop:config>
    <!--  切入點:我們需要在哪裏增強,需要執行的位置  -->
    <aop:pointcut id="pointCut" expression="execution(* com.ara.service.UserServiceImpl.*(..))"/>

    <!--  配置切入增強  -->
    <aop:advisor advice-ref="beforeLog" pointcut-ref="pointCut"/>
    <aop:advisor advice-ref="afterLog" pointcut-ref="pointCut"/>
</aop:config>

配置好後,我們再對測試代碼進行測試,發現結果如下:
在這裏插入圖片描述
發現得到了我們想要的結果。

2.方式二:自定義類

編寫切入代碼:

package com.ara.diy;

public class DiyPointCut {

    public void before(){
        System.out.println("========before()========");
    }

    public void after(){
        System.out.println("========after()========");
    }

}

在Spring配置文件中配置:

<!--  方式二:自定義類  -->
<bean id="diy" class="com.ara.diy.DiyPointCut"/>

<aop:config>
    <!--  自定義切面,ref要引用的類  -->
    <aop:aspect ref="diy">
        <!--  切入點  -->
        <aop:pointcut id="point" expression="execution(* com.ara.service.UserServiceImpl.*(..))"/>

        <!--  通知  -->
        <aop:before method="before" pointcut-ref="point"/>
        <aop:after method="after" pointcut-ref="point"/>
    </aop:aspect>
</aop:config>

測試結果如下:
在這裏插入圖片描述
同樣也得到了我們想要的結果

3.方式三:使用註解方式

編寫切入類:

package com.ara.diy;

//方式三:使用註解方式實現AOP

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect //標註這個類是一個切面
public class AnnotationPointCut {

    //前置通知
    @Before("execution(* com.ara.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("========before()========");
    }

    //後置通知
    @After("execution(* com.ara.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("========after()========");
    }

    //在環繞增強中,我們可以給定一個參數 代表我們要獲取處理切入的點(相當於過濾器)
    @Around("execution(* com.ara.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("========around-before()========");

        System.out.println(point.getSignature());

        //執行:相當於過濾器的放行
        Object proceed = point.proceed();

        System.out.println("========around-after()========");
        System.out.println(proceed);

    }

}

在Spring配置文件中添加:

<!--  方式三:註解類  -->
<bean id="annotationPointCut" class="com.ara.diy.AnnotationPointCut" />
<!--  開啓切面註解支持  -->
<aop:aspectj-autoproxy/>

然後再對代碼進行測試,結果如下:
在這裏插入圖片描述

這樣我們也能得到對應的效果。


至於我們需要選擇哪種方式實現,則根據實際需求選擇即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章