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>

 

转载于:https://www.cnblogs.com/axu521/p/10142414.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值