zoukankan      html  css  js  c++  java
  • Spring 学习——Spring AOP——AOP配置篇Advice(无参数传递)

    声明通知Advice

    • 配置方式(以前置通知为例子)
      • 方式一
        <aop:config>
        <aop:aspect id="ikAspectAop" ref="ikAspect">
        <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut>
        <aop:before method="aspectBefore"
        pointcut-ref="ikPoint"></aop:before>
        </aop:aspect>
        </aop:config>
        • 优点:前置通知、后置通知、环绕通知使用同一个切点时,配置一个<aop:poincut />即可,方便配置。
      • 方式二
        <aop:config>
                <aop:aspect id="ikAspectAop" ref="ikAspect">
                        <aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before>
                </aop:aspect>
        </aop:config>
        • 优点:前置通知、后置通知、环绕通知使用不同切点时,不需要配置<aop:poincut />元素,可以直接在<aop:before />元素内配置切点。
      • Next
         
    • 前置通知(Before Advice)
      • 在切入点时机事务执行之前,执行通知
      • 方式一
         <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut>
                  <aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
             </aop:aspect>
         </aop:config>
      • 方式二
         <aop:config>
              <aop:aspect id="ikAspectAop" ref="ikAspect">
                   <aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before>
              </aop:aspect>
         </aop:config>
      • Next
    • 后置通知(After Running Advice)
      • 在切入点时机事务执行之后,执行通知(前提:切入点执行时没有异常,否则不会执行
      • 方式一
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut>
                  <aop:after-returning method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
             </aop:aspect>
         </aop:config>
      • 方式二
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:after-returning method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before>
             </aop:aspect>
         </aop:config>
      • Next
    • 抛出异常通知(After Throwing Advice)
      • 若在切入点时机执行时抛出异常,执行通知。(执行异常通知,则不会执行后置通知
      • 方式一
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut>
                  <aop:after-throwing method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
             </aop:aspect>
         </aop:config>
      • 方式二
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:after-throwing method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before>
             </aop:aspect>
         </aop:config>
      • Next
    • 后通知(After Ending Advice)
      • 在切入点时机执行之后,执行通知。(切入点执行时,无论正常执行,还是抛出异常,都会执行通知,相当于try_catch_finally)。
      • 方式一
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut>
                  <aop:after method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
             </aop:aspect>
         </aop:config>
      • 方式二
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:after method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before>
             </aop:aspect>
         </aop:config>
      • Next
      • 无论切面是否出现异常,后通知动作正常执行
    • 环绕通知(Around Advice)
      • 环绕通知在切入点时机前后都可以执行通知。(在切面类中的通知方法中,默认存在参数ProceedingJoinPoint pj
      • 方式一
        <?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:context="http://www.springframework.org/schema/context"
        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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd" >

        <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean>
        <bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean>

        <aop:config>
        <aop:aspect id="ikAspectAop" ref="ikAspect">
        <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut>
        <aop:around method="aspectAround" pointcut-ref="ikPoint"></aop:around>
        </aop:aspect>
        </aop:config>

        </beans>
        package com.jing.spring.aop;
        import org.aspectj.lang.ProceedingJoinPoint;
        /**
        *切面类
        */
        public class IKAspect { public void aspectAround(ProceedingJoinPoint pj){ System.out.println("IKAspect.aspectAroud,its 环绕通知前"); try { Object proceed = pj.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("IKAspect.aspectAroud,its 环绕通知后"); } }
      • 方式二
        <?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:context="http://www.springframework.org/schema/context"
               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/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd" >
        
                <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean>
                <bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean>
        
                <aop:config>
                    <aop:aspect id="ikAspectAop" ref="ikAspect">
                        <aop:around method="aspectAround" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:around>
                    </aop:aspect>
                </aop:config>
        
        </beans>
        package com.jing.spring.aop;
        import org.aspectj.lang.ProceedingJoinPoint;
        /**
        *切面类
        */
        public class IKAspect { public void aspectAround(ProceedingJoinPoint pj){ System.out.println("IKAspect.aspectAroud,its 环绕通知前"); try { Object proceed = pj.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("IKAspect.aspectAroud,its 环绕通知后"); } }
      • Next
  • 相关阅读:
    DeFi之道丨科普:一分钟了解以太坊layer2扩容
    4.B-Air302(NB-IOT)-功能扩展-Android扫码绑定Air302,并通过MQTT实现远程控制和监控PLC(三菱Fx1s-10MR)
    Air302(NB-IOT)-硬件BUG-电路硬件错误及其修改说明
    4.2-Air302(NB-IOT)-自建MQTT服务器-Android扫码绑定Air302,并通过MQTT实现远程通信控制
    Android 开发:数据库操作-android 使用 litepal 操作本地数据库
    0.5-Air302(NB-IOT)-连接自建MQTT服务器
    0.1-Air302(NB-IOT)-刷AT指令固件
    STM32+ESP8266+AIR202基本控制篇-00-开发板回收说明
    21-STM32+ESP8266+AIR202/302远程升级方案-扩展应用-移植远程升级包实现STM32F407VET6+串口网络模组(ESP8266/Air202/Air302)使用http或者https远程升级单片机程序
    数据处理思想和程序架构: 单片机STM32F407xx/F405xx/F415xx/417xx系列flash存储方案
  • 原文地址:https://www.cnblogs.com/zuiyue_jing/p/10432032.html
Copyright © 2011-2022 走看看