SpringAOP四种通知类型+环绕通知说明

 更新时间:2021年09月07日 09:13:25   作者:晚点喝可乐z  
这篇文章主要介绍了SpringAOP四种通知类型+环绕通知说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

SpringAOP的四种通知类型:前置通知、异常通知、后置通知、异常通知

一、四种常见的通知类型

给出 账户的业务层接口 IAccountService.java,

为了便于演示这四种通知类型,我们就只留下了一个方法。

public interface IAccountService {
   void saveAccount();
}

给出 账户的业务层接口的实现类 AccountServiceImpl.java

public class AccountServiceImpl implements IAccountService{
    @Override
    public void saveAccount() {
        System.out.println("执行了保存");
        //int i=1/0;
    }
}

给出一个日志类, 用于打印日志

public class Logger {
    /**
     * 前置通知
     */
    public  void beforePrintLog(){
        System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
    }
    /**
     * 后置通知
     */
    public  void afterReturningPrintLog(){
        System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
    }
    /**
     * 异常通知
     */
    public  void afterThrowingPrintLog(){
        System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
    }
    /**
     * 最终通知
     */
    public  void afterPrintLog(){
        System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
    }
}

给出配置信息bean.xml

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 配置srping的Ioc,把service对象配置进来-->
    <bean id="accountService" class="service.AccountServiceImpl"></bean>
    <!-- 配置Logger类 -->
    <bean id="logger" class="utils.Logger"></bean>
    <!--配置AOP-->
    <aop:config>
      <!--配置切入点表达式 -->
        <aop:pointcut id="pt1" expression="execution(* service.AccountServiceImpl.saveAccount())"></aop:pointcut>
        <!--配置切面 -->
        <aop:aspect id="logAdvice" ref="logger">
            <!-- 配置前置通知:在切入点方法执行之前执行-->
            <aop:before method="beforePrintLog" pointcut-ref="pt1" ></aop:before>
            <!-- 配置后置通知:在切入点方法正常执行之后值-->
            <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>
            <!-- 配置异常通知:在切入点方法执行产生异常之后执行-->
            <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>
            <!-- 配置最终通知:无论切入点方法是否正常执行它都会在其后面执行-->
            <aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>
            
        </aop:aspect>
    </aop:config>
</beans>

注意

1)异常通知和后置通知永远只能执行一个

2)配置切入点表达式

此标签写在aop:aspect标签内部只能当前切面使用。

它还可以写在aop:aspect外面,此时就变成了所有切面可用

给出Test类

public class AOPTest {
    public static void main(String[] args) {
        //1.获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.获取对象
        IAccountService as = (IAccountService)ac.getBean("accountService");
        //3.执行方法
       as.saveAccount();
    }
}

执行结果:

在这里插入图片描述

当我们放开AccountServiceImpl类中我们故意制造的异常 int i=1/0;时:

在这里插入图片描述

二、环绕通知

环绕通知,只需要稍稍微改变上面例子的两点即可

1、改动日志类 Logger.java

public class Logger {
    public Object aroundPringLog(ProceedingJoinPoint pjp){
        Object rtValue = null;
        try{
            Object[] args = pjp.getArgs();//得到方法执行所需的参数
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置");
            rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置");
            return rtValue;
        }catch (Throwable t){
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常");
            throw new RuntimeException(t);
        }finally {
            System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终");
        }
    }
}

注意:pjp.proceed(args)会报异常,必须用 Throwable t,因为Exception拦不住它

2、改动配置文件

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 配置srping的Ioc,把service对象配置进来-->
    <bean id="accountService" class="service.AccountServiceImpl"></bean>
    <!-- 配置Logger类 -->
    <bean id="logger" class="utils.Logger"></bean>
    <!--配置AOP-->
    <aop:config>
    	<!--配置切入点表达式 -->
        <aop:pointcut id="pt1" expression="execution(* service.AccountServiceImpl.saveAccount())"></aop:pointcut>
        <!--配置切面 -->
        <aop:aspect id="logAdvice" ref="logger">
            <!-- 配置环绕通知 详细的注释请看Logger类中-->
            <aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around>
        </aop:aspect>
    </aop:config>
</beans>

分析

  • spring中的环绕通知是spring框架为我们提供的一种可以在代码中手动控制增强方法何时执行的方式。
  • Spring框架为我们提供了一个接口:ProceedingJoinPoint。该接口有一个方法proceed(),此方法就相当于明确调用切入点方法。该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。

AOP机制之环绕通知的见解

我们都知道,AOP机制的核心是在不修改源码的基础上对业务层方法的增强。

其中有五个通知类型

  • 1、前置通知:在切入点方法之前执行
  • 2、后置通知:在切入点方法之后通知
  • 3、异常通知:在执行切入点方法过程中出现异常后执行(因此异常通知和后置通知只能执行一个)
  • 4、最终通知:无论切入点方法是否正常执行它都会执行
  • 5、环绕通知: 当配置环绕通知之后,在环绕通知里面必须要明确调用业务层的方法,如果不调用,就会出现只出现通知,而不执行方法。其中原理和动态代理是一样的,代码如下。
public AccountService getAccountService() {
        return (AccountService) Proxy.newProxyInstance(accountService.getClass().getClassLoader(), accountService.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                Object rtValue=null;
                try {
                    //1、开启事务
                    txManager.beginTransaction();
                    //2、执行操作,整个过程像对每个方法进行了包装,并返回新的accountService对象
                    rtValue=method.invoke(accountService,objects);
                    //3、提交事务
                    txManager.commit();
                    //4、返回结果
                    return rtValue;
                }catch (Exception e){
                    //5、回滚事务
                    txManager.rollback();
                    throw new RuntimeException(e);
                }finally {
                    //6、释放连接
                    txManager.release();
                }
            }
        });
    }

如果不明确调用业务层方法,就像一个画皮,没有发挥本质的作用。

除此之外,我认为环绕通知可以代替其他的四个通知,

public Object aroundPrintLog(ProceedingJoinPoint pjp){//环绕通知是不是能够代替其他的通知
        Object rtvalue=null;
        try {
            /**
             *这里的一切都是为都是给业务层方法进行增强,例如:把那些方法拿过来,然后核心的还是rtvalue=pjp.proceed(args),
             *其他的输出只不过是为核心的业务层方法进行修饰
             */
            Object[] args=pjp.getArgs();//得到方法执行所需的参数
            System.out.println("前置通知");
            rtvalue=pjp.proceed(args);//明确调用业务层方法
            System.out.println("后置通知");
            return rtvalue;
        }catch (Throwable e){
            System.out.println("异常通知");
            throw new RuntimeException(e);
        }finally {
            System.out.println("最终通知");
        }
    }

这个属于典型的环绕通知,其中把输出方法换成相应的通知方法就可以(有不同观点的可以说出来一起讨论)。

最后,分享一下我学这个知识的方法。方法增强,本质上就是对源码不修改的情况下进行方法的加工。就好像烤羊肉串一样,其中的核心就是羊肉,就像公司给你的业务层方法(这个是不让修改的)。在给顾客使用前,收先对羊肉进行刷油、烤、撒料一系列过程,这一过程就是我们对业务层方法的增强,使业务层的功能更加健壮,对应的烧烤也就是更美味。核心的一点就是正确调用业务层的方法,不管在哪类通知中,都能对业务层方法进行正确、有效地增强

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 关于Spring Boot动态权限变更问题的实现方案

    关于Spring Boot动态权限变更问题的实现方案

    这篇文章主要介绍了Spring Boot动态权限变更实现的整体方案使用session作为缓存,结合AOP技术进行token认证和权限控制,本文给大家介绍的非常详细,需要的朋友参考下吧
    2021-06-06
  • Maven项目更换本地仓库过程图解

    Maven项目更换本地仓库过程图解

    这篇文章主要介绍了Maven项目更换本地仓库过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • springboot2.0整合logback日志的详细代码

    springboot2.0整合logback日志的详细代码

    这篇文章主要介绍了springboot2.0整合logback日志的应用场景分析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • SpringMVC实现前端后台交互传递数据

    SpringMVC实现前端后台交互传递数据

    本篇文章主要介绍了SpringMVC实现前端后台传递数据的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03
  • 使用SpringSecurity设置角色和权限的注意点

    使用SpringSecurity设置角色和权限的注意点

    这篇文章主要介绍了使用SpringSecurity设置角色和权限的注意点,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • SpringBoot整合Redis实现登录失败锁定功能(实例详解)

    SpringBoot整合Redis实现登录失败锁定功能(实例详解)

    本文我们已经探讨如何利用Redis来实现锁定账户的安全措施,以及通过SpringBoot整合Redis实现了这一功能,感兴趣的朋友跟随小编一起学习下吧
    2024-02-02
  • SpringBoot生成二维码的实现

    SpringBoot生成二维码的实现

    这篇文章主要介绍了SpringBoot生成二维码的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Spring Boot Shiro auto-configure工作流程详解

    Spring Boot Shiro auto-configure工作流程详解

    这篇文章主要为大家介绍了Spring Boot Shiro auto-configure工作流程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Java LinkedList集合功能实例解析

    Java LinkedList集合功能实例解析

    这篇文章主要介绍了Java LinkedList集合功能实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 详解Nacos配置中心的实现

    详解Nacos配置中心的实现

    Spring Cloud Alibaba 是阿里巴巴提供的一站式微服务开发解决方案。而 Nacos 作为 Spring Cloud Alibaba 的核心组件之一,提供了两个非常重要的功能:注册中心和配置中心,我们今天来了解和实现一下二者
    2022-08-08

最新评论