日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

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

作者:一眉程序猿 更新時間: 2022-05-20 編程語言

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

準備環境如下:

  • 導入對應依賴:

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>5.2.4.RELEASEversion>
dependency>


<dependency>
    <groupId>org.aspectjgroupId>
    <artifactId>aspectjweaverartifactId>
    <version>1.9.5version>
dependency>


<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.12version>
dependency>
  • 準備Spring的配置文件,applicationContext.xml,一定要引入AOP的約束

<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配置文件中配置切面:



<bean id="beforeLog" class="com.ara.log.BeforeLog"/>
<bean id="afterLog" class="com.ara.log.AfterLog"/>

<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>
    
    <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/>

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

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


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

原文鏈接:https://blog.csdn.net/weixin_45935633/article/details/104830781

欄目分類
最近更新