使用顾问增加前置增强和后置增强
<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值要多个的话使用逗号隔开。
结果: