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

然后再对代码进行测试,结果如下:
在这里插入图片描述

这样我们也能得到对应的效果。


至于我们需要选择哪种方式实现,则根据实际需求选择即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值