zoukankan      html  css  js  c++  java
  • 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、控制台

       

  • 相关阅读:
    unreal-python-howtos
    vscode plugin development
    [uva] 1671 History of Languages
    [codeup] 1128 出租车费
    [codeup] 1126 看电视
    Ubuntu 16.04 + ROS Kinetic 机器人操作系统学习镜像分享与使用安装说明
    (二)ROS系统架构及概念 ROS Architecture and Concepts 以Kinetic为主更新 附课件PPT
    ROS新功能包PlotJuggler绘图
    Winform DevExpress控件库(三) 使用NavBarControl控件定制导航栏
    数据意识崛起,从企业应用看BI软件的未来发展
  • 原文地址:https://www.cnblogs.com/Zzzzn/p/11758910.html
Copyright © 2011-2022 走看看