zoukankan      html  css  js  c++  java
  • AOP 环绕通知 (Schema-base方式) 和 AspectJ方式在通知中获取切点的参数

    环绕通知(Schema- base方式)

      1、把前置通知和后置通知都写到一个通知中,组成了环绕通知

      2、实现步骤:

        2.1 新建一个类实现 MethodInterceptor 接口

    public class MyArround implements MethodInterceptor{
        @Override
        public Object invoke(MethodInvocation arg0) throws Throwable {
            System.out.println("环绕--前置通知11111");
            Object result = arg0.proceed();//放行,调用切点方式
            System.out.println("环绕--后置通知222");
            return result;
        }   
    }

        2.2 配置applicationContext.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">
            
            <bean id="myarround" class="com.bjsxt.advice.MyArround"></bean>
            <bean id="demo" class="com.bjsxt.test.Demo"></bean>
            <aop:config>
                <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" id="mypoint"/>
                <aop:advisor advice-ref="myarround" pointcut-ref="mypoint"/>
            </aop:config>
            
      </beans>

     通过AspectJ方式获取在通知中获取切点的参数

      1、新建类

    public class Advice {
        public void mybefore(String name1,int age1){
            System.out.println("前置通知"+name1+" "+age1);
        }
        public void myafter(){
            System.out.println("后置通知1");
        }
        public void myafterint(){
            System.out.println("后置通知222");
        }
        public void mythrow(){
            System.out.println("触发异常");
        }
    }

      2、在spring中配置ApplicationContext.xml

          2.1、<aop:after/>:后置通知,不管切点是否出现异常,都执行

          2.2、<aop:after-returning/>:后置通知,切点出现异常,就不会执行

          2.3、<aop:after/> 和<aop:returnint> 和<aop:after-throwing> 的执行顺序和 配置顺序有关

          2.4、不能使用 && 

          2.5、args(名称) 名称自定义,但是顺序和demo1(参数,参数) 对应

          2.6、<aop:before> 中 arg-names=" 名称 "  名称来源于execution中args

    <?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">
            
            <bean id="demo" class="com.bjsxt.test.Demo"></bean>
            <bean id="advice" class="com.bjsxt.advice.Advice"></bean>
            
            <aop:config>
                <aop:aspect ref="advice">                                      args 中有几个参数 下面的arg-names就要有几个参数
                    <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1(String,int)) and args(name1,age1)" id="mypoint"/>  
                    <aop:before method="mybefore" pointcut-ref="mypoint"  arg-names="name1,age1" />      arg-names中的参数名,要和通知类中的方式的参数名一致
    <!--            <aop:after method="myafter" pointcut-ref="mypoint"/>
                    <aop:after-returning method="myafterint" pointcut-ref="mypoint"/>
                    <aop:after-throwing method="mythrow" pointcut-ref="mypoint"/> -->
                </aop:aspect>
            </aop:config>
        
      </beans>
    好好学习,天天向上。 努力工作,给自己的和家人一个满意的答案。。。。
  • 相关阅读:
    《人月神话》阅读笔记02
    学习进度条-4
    课堂作业3
    MySql 8.0及对应驱动包匹配注意点
    关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
    STM32之ADC配置,ADC_Mode模式理解
    STM32串口中断的方式发送
    RTC 与RCC 之间的关系
    stm32 MDK5软件仿真之查看io口输出
    STM32 PWM学习
  • 原文地址:https://www.cnblogs.com/axu521/p/10142414.html
Copyright © 2011-2022 走看看