Spring的增强模式

一、前置增强

  1、IdoSomeService

    

   2、IdoSomeServiceImpl类实现IdoSomeService接口

    

   3、MyBeforeAdvice 实现前置增强方法

    

   4、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"
       xmlns:p="http://www.springframework.org/schema/p"
       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 id="idoSomeService1" class="cn.spring.proxyfactory.IdoSomeServiceImpl"></bean>
    <!--增强:切面-->
    <bean id="myBeforeAdvice" class="cn.spring.proxyfactory.MyBeforeAdvice"></bean>
    <!--使用代理工厂实现增强-->
    <bean id="proxyFactory1" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--将增强和业务织入到一起-->
        <property name="target" ref="idoSomeService1"></property>
        <!--拦截增强类-->
        <property name="interceptorNames" value="myBeforeAdvice"></property>
        <!--更换代理方式    proxyTargetClass默认值为false 即JDK动态代理,但是当目标对象没有接口时,自动改为CGLIB-->
        <property name="proxyTargetClass" value="true"></property>
    </bean>
</
beans>

  5、测试类

    

  6、控制台

    

二、环绕增强

  1、IdoSomeService

    

  2、IdoSomeServiceImpl类实现IdoSomeService接口

    

  3、MyAroundAdvice 实现环绕增强方法

    

   4、applicationContext.xml配置文件   

 <!--环绕增强实现-->
    <!--注入业务Bean-->
    <bean id="idoSomeService2" class="cn.spring.around.IdoSomeServiceImpl"></bean>
    <!--增强:切面-->
    <bean id="myAroundAdvice" class="cn.spring.around.MyAroundAdvice"></bean>
    <!--使用代理工厂实现增强-->
    <bean id="proxyFactory2" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--将增强和业务织入到一起-->
        <property name="target" ref="idoSomeService2"></property>
        <!--拦截增强类-->
        <property name="interceptorNames" value="myAroundAdvice"></property>
        <!--更换代理方式    proxyTargetClass默认值为false 即JDK动态代理,但是当目标对象没有接口时,自动改为CGLIB-->
        <property name="proxyTargetClass" value="true"></property>
    </bean>

  5、测试类

    

   

  6、控制台

    

三、异常增强 

  1、IdoSomeService

    

  

  2、IdoSomeServiceImpl类实现IdoSomeService接口

    

  

  3、MyThrowAdvice实现异常增强

    

  4、applicationContext.xml配置文件

<!--异常增强实现-->
    <!--注入业务Bean-->
    <bean id="idoSomeService3" class="cn.spring.throwadvice.IdoSomeServiceImpl"></bean>
    <!--增强:切面-->
    <bean id="myThrowAdvice" class="cn.spring.throwadvice.MyThrowAdvice"></bean>
    <!--使用代理工厂实现增强-->
    <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--将增强和业务织入到一起-->
        <property name="target" ref="idoSomeService3"></property>
        <!--拦截增强类-->
        <property name="interceptorNames" value="myThrowAdvice"></property>
        <!--更换代理方式    proxyTargetClass默认值为false 即JDK动态代理,但是当目标对象没有接口时,自动改为CGLIB-->
        <property name="proxyTargetClass" value="true"></property>
    </bean>

  5、测试类

   

  6、控制台   

   

四、最终增强

  1、IdoSomeService

    

  

  2、IdoSomeServiceImpl类实现IdoSomeService接口

    

  

  3、MyThrowAdvice实现最终增强

    

  4、applicationContext.xml配置文件

<!--最终增强实现-->
    <!--注入业务Bean-->
    <bean id="idoSomeService4" class="cn.spring.throwadvice.IdoSomeServiceImpl"></bean>
    <!--增强:切面-->
    <bean id="myThrowAdvice1" class="cn.spring.throwadvice.MyThrowAdvice"></bean>
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
        <aop:aspect ref="myThrowAdvice1">
            <aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
            <aop:after method="afterAdvice" pointcut-ref="pointcut"></aop:after>
        </aop:aspect>
    </aop:config>

  5、测试类

    

  6、控制台

   

原文地址:https://www.cnblogs.com/Zzzzn/p/11758910.html