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、控制台

       

  • 相关阅读:
    paip.数据库全文检索 attilax总结
    软件网站安全性的设计与检测与解决方案
    防止SQL注入解决方案
    paip.账务系统的安全性
    快速开发字段很多的MIS表
    paip.php调试不能显示局部变量内容w/ xdebug
    程序安全性之配置文件安全
    paip.VS2010未能加载类型
    paip.盘古汉字转拼音组件库使用总结
    paip.跟踪DISCUZ积分日志功能总结
  • 原文地址:https://www.cnblogs.com/Zzzzn/p/11758910.html
Copyright © 2011-2022 走看看