码迷,mamicode.com
首页 > 编程语言 > 详细

Spring的增强模式

时间:2019-10-29 15:36:27      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:ram   get   exe   spec   方法   express   asp   framework   com   

一、前置增强

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

   技术图片

 

Spring的增强模式

标签:ram   get   exe   spec   方法   express   asp   framework   com   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!