一、前置增强
beans.xml(使用JDK代理)
<?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-3.0.xsd">
<!-- 增强类-->
<bean id="greetingAdvice" class="com.testApi.beforeAdvice.GreetingBeforeAdvice"/>
<bean id="target2" class="com.testApi.beforeAdvice.SuperWaiter"/>
<bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"
p:proxyInterfaces="com.testApi.beforeAdvice.Waiter"
p:interceptorNames="greetingAdvice"
p:target-ref="target2"/>
</beans>
<!-- proxyInterfaces指明代理要实现对接口
target2 是要代理对目标类
interceptorNames 是需要植入目标对象的Bean列表,必须是实现类MethodInterceptor或者Advisor的类
optimize 为true时强制实行CGLIB代理
proxyTargetClass 是否对类进行代理,boolean类型
-->
beans.xml(使用CGLIB代理)
<?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-3.0.xsd">
<!-- 增强类-->
<bean id="greetingAdvice" class="com.testApi.beforeAdvice.GreetingBeforeAdvice"/>
<bean id="target2" class="com.testApi.beforeAdvice.SuperWaiter"/>
<bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"
p:interceptorNames="greetingAdvice"
p:target-ref="target2"
p:proxyTargetClass="true"/>
</beans>
<!-- proxyInterfaces指明代理要实现对接口
target2 是要代理对目标类
interceptorNames 是需要植入目标对象的Bean列表,必须是实现类MethodInterceptor或者Advisor的类
optimize 为true时强制实行CGLIB代理
proxyTargetClass 是否对类进行代理,boolean类型
-->
一节接口需要多个增强对写法:
p:interceptorNames="aAdvice,bAdvice,cAdvice"
测试类:
@Test
public void test3(){
String path= "beans.xml";
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(path);
Waiter waiter=(Waiter)context.getBean("waiter");
waiter.greetingTo("hehe");
waiter.serveTo("liming");
}
二、后置增强
<?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-3.0.xsd">
<!-- 增强类-->
<bean id="greetingAdvice" class="com.testApi.beforeAdvice.GreetingBeforeAdvice"/>
<bean id="afterAdvice" class="com.testApi.beforeAdvice.GreetingAfterAdvice"/>
<bean id="target2" class="com.testApi.beforeAdvice.SuperWaiter"/>
<bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"
p:interceptorNames="greetingAdvice,afterAdvice"
p:target-ref="target2"
p:proxyTargetClass="true"/>
</beans>
<!-- proxyInterfaces指明代理要实现对接口
target2 是要代理对目标类
interceptorNames 是需要植入目标对象的Bean列表,必须是实现类MethodInterceptor或者Advisor的类
optimize 为true时强制实行CGLIB代理
proxyTargetClass 是否对类进行代理,boolean类型
-->