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 ></bean>
        <bean ></bean>
        <aop:config>
            <aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo1())" />
            <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 ></bean>
        <bean ></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)" />  
                <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>