< value>com.denny_blue.springdemo.aop.BuyBook< /value>
< /property>
< property name="interceptorNames">
< list>
< value>myBeforeAdvice< /value>
< value>myAfterAdvice< /value>
< value>myAroundAdvice< /value>
< value>myThrowsAdvice< /value>
< /list>
< /property>
< property name="target">
< ref bean="myBuyBookTarget"/>
< /property>
< /bean>
< /beans>
我们先声明所有的bean,通过
< bean id="myBuyBookTarget" class="com.denny_blue.springdemo.aop.MyBuyBook"/>
确定将要被增强的对象??目标对象(target),我们可以很容易地替换这个目标对象,只要它实现业务接口。代理的接口通过:
< property name="proxyInterfaces">
< value>com.denny_blue.springdemo.aop.BuyBook< /value>
< /property>
设定,然后是要用到一系列增强,注意,顺序是很有影响的!你可以尝试着改变顺序看看结果:)
< property name="interceptorNames">
< list>
< value>myBeforeAdvice< /value>
< value>myAfterAdvice< /value>
< value>myAroundAdvice< /value>
< value>myThrowsAdvice< /value>
< /list>
< /property>
一切准备好了,我们来测试吧,GO GO GO
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.denny_blue.springdemo.aop.BuyBook;
public class TestAdvice {
public static void main(String args[]){
ApplicationContext ctx=new FileSystemXmlApplicationContext("/src/beans.xml");
//我的beans.xml放在项目下的src目录,eclipse环境下,请自己调整
BuyBook buybook=(BuyBook)ctx.getBean("buyBook");
buybook.buyBook("jordan","< 深入浅出hibernate>");
// buybook.buyBook("dennis","< spring in action>"); //去掉注释即可观察异常增强
// buybook.buyBook("jordan","< 深入浅出hibernate>"); //去掉注释即可观察环绕增强
}
}
文章来源于领测软件测试网 https://www.ltesting.net/