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

Spring 通知和顾问进行增强

时间:2018-11-24 14:27:57      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:void   oca   ntc   public   需要   err   org   测试方法   结果   

使用顾问增加前置增强和后置增强

<bean id="1" class="目标对象"></bean>

<bean id="2" class="代理对象"></bean>

<bean id="3" class="顾问">
<property name="5" ref="代理对象id">
<property name="6" value="匹配的规则">
</bean>

<bean id="4" class="代理工厂Bean">
<property name="7" ref="目标对象">
<property name="8" value="顾问id">
</bean>

前置增强:

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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--实现类(目标对象)-->
    <bean id="SSI" class="cn.Spring.Day16Advisor.ServiceSqlImpl"></bean>
    <!--增强类(代理对象)-->
    <bean id="MMBA" class="cn.Spring.Day16Advisor.MyMethodBeforeAdvice"></bean>
    <!--顾问-->
    <bean id="advisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="MMBA"></property><!--传的是代理对象-->
        <property name="mappedNames" value="*s*"></property><!--匹配的方法-->
     </bean>


    <!--代理工厂Bean-->
    <bean id="MyProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="SSI"></property><!--接收目标对象-->
        <property name="interceptorNames" value="advisor"></property><!--然后把顾问拿进来-->
    </bean>
</beans>

测试方法代码:

import cn.Spring.Day16Advisor.IServiceSql;
import cn.Spring.Day16Advisor.ServiceSqlImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test20181124 {
    @Test
    public void test1(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationAdvisor.xml");
        IServiceSql bean =(IServiceSql) applicationContext.getBean("MyProxy");//传入代理工厂Bean的id
        bean.delete();
        bean.insert();
        bean.select();
        bean.update();
    }
}

 

代理类:

技术分享图片

 

技术分享图片

 

 前置加后置增强:

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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--实现类(目标对象)-->
    <bean id="SSI" class="cn.Spring.Day16Advisor.ServiceSqlImpl"></bean>
    <!--增强类(代理对象)-->
    <bean id="MMBA" class="cn.Spring.Day16Advisor.MyMethodBeforeAdvice"></bean>
    <!--顾问-->
    <bean id="advisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="MMBA"></property><!--传的是代理对象-->
        <property name="mappedNames" value="*s*"></property><!--匹配的方法-->
     </bean>

    <!--增强类(代理对象)-->
    <bean id="MARA" class="cn.Spring.Day16Advisor.MyAfterReturningAdvice"></bean>
    <!--顾问-->
    <bean id="advisorT" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="MARA"></property><!--传的是代理对象-->
        <property name="mappedNames" value="*s*"></property><!--匹配的方法-->
    </bean>
    <!--代理工厂Bean-->
    <bean id="MyProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="SSI"></property><!--接收目标对象-->
        <property name="interceptorNames" value="advisor,advisorT"></property><!--然后把顾问拿进来-->
    </bean>
</beans>

需要注意的是在代理工厂Bean中interceptorNames的value值要多个的话使用逗号隔开。

结果:

技术分享图片

 

Spring 通知和顾问进行增强

标签:void   oca   ntc   public   需要   err   org   测试方法   结果   

原文地址:https://www.cnblogs.com/java-263/p/10011336.html

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