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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Spring-實(shí)現(xiàn)AOP的三種方式演示

作者:一眉程序猿 更新時(shí)間: 2022-05-20 編程語(yǔ)言

對(duì)于AOP,我們明確的知道,是Spring的核心之一,這里,博主分享Spring實(shí)現(xiàn)AOP的三種方式。

準(zhǔn)備環(huán)境如下:

  • 導(dǎo)入對(duì)應(yīng)依賴:

<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>
  • 準(zhǔn)備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>

準(zhǔn)備一個(gè)模擬業(yè)務(wù)層service,其中包括UserService接口和UserServiceImpl實(shí)現(xiàn)類:
UserService接口:

package com.ara.service;

//模擬增刪查改的業(yè)務(wù)層
public interface UserService {

    void add();

    void delete();

    void update();

    void query();

}

UserServiceImpl實(shí)現(xiàn)類:

package com.ara.service;

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

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

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

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

將業(yè)務(wù)實(shí)現(xiàn)的bean添加到Spring配置中:

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

測(cè)試代碼:

@Test
public void springTest(){

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

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

    userService.add();

}

測(cè)試代碼一直保持不變,就更能體現(xiàn)出AOP的思想了。
這樣直接調(diào)用的結(jié)果為:
在這里插入圖片描述
現(xiàn)在的需求是:在不改動(dòng)上述代碼的前提下,我們調(diào)用其中的方法會(huì)打印出對(duì)應(yīng)的切入信息。

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

編寫切入類:

BeforeLog:

package com.ara.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

//方法前置通知 實(shí)現(xiàn)Spring框架的方法前置通知接口
public class BeforeLog implements MethodBeforeAdvice {


    /**
     * @param method 要執(zhí)行的目標(biāo)方法的對(duì)象
     * @param args   參數(shù)列表
     * @param target 目標(biāo)對(duì)象
     * @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;

//方法前置通知 實(shí)現(xiàn)Spring框架的方法后置通知接口
public class AfterLog implements AfterReturningAdvice {
    /**
     * @param returnValue 返回值
     * @param method      執(zhí)行了什么方法
     * @param args        執(zhí)行方法的參數(shù)
     * @param target      執(zhí)行的目標(biāo)
     * @throws Throwable
     */
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {

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

    }
}

然后我們?cè)赟pring配置文件中配置切面:



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

配置好后,我們?cè)賹?duì)測(cè)試代碼進(jìn)行測(cè)試,發(fā)現(xiàn)結(jié)果如下:
在這里插入圖片描述
發(fā)現(xiàn)得到了我們想要的結(jié)果。

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>

測(cè)試結(jié)果如下:
在這里插入圖片描述
同樣也得到了我們想要的結(jié)果

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

編寫切入類:

package com.ara.diy;

//方式三:使用注解方式實(shí)現(xiàn)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 //標(biāo)注這個(gè)類是一個(gè)切面
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()========");
    }

    //在環(huán)繞增強(qiáng)中,我們可以給定一個(gè)參數(shù) 代表我們要獲取處理切入的點(diǎn)(相當(dāng)于過(guò)濾器)
    @Around("execution(* com.ara.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("========around-before()========");

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

        //執(zhí)行:相當(dāng)于過(guò)濾器的放行
        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/>

然后再對(duì)代碼進(jìn)行測(cè)試,結(jié)果如下:
在這里插入圖片描述

這樣我們也能得到對(duì)應(yīng)的效果。


至于我們需要選擇哪種方式實(shí)現(xiàn),則根據(jù)實(shí)際需求選擇即可。

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

欄目分類
最近更新