学无先后,达者为师

网站首页 编程语言 正文

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

栏目分类
最近更新