AOP 环绕通知

前端之家收集整理的这篇文章主要介绍了AOP 环绕通知前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

 

环绕通知(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>

猜你在找的XML相关文章