ITeye博客

Spring AOP系列之三:环绕代理

czj4451 · 2014-04-13 18:20 · spring/ejb · 编程语言 · Spring · AOP · interceptor · Method · Markdown

通过实现[color=red]org.aopalliance.intercept.MethodInterceptor[/color]接口来实现环绕通知: [code="java"] public class CarAroundProxy implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("Skoda 4S shop"); Object result = invocation.proceed(); System.out.println("Give 200 maintenance coupon via around proxy"); return result; } } [/code] Spring配置文件beans-around-proxy.xml: [code="xml"] com.john.spring.aop.Vehicle carAroundProxy [/code] 测试: [code="java"] public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "beans-around-proxy.xml" }); Vehicle vehicle = (Vehicle) ctx.getBean("proxyBean"); vehicle.info(); } [/code] 输出: Skoda 4S shop Car name: Superb, price: 220000 Give 200 maintenance coupon via around proxy 调试程序,vehicle使用JDK的代理生成: [img]http://dl2.iteye.com/upload/attachment/0096/0586/dd3e351b-8f62-3618-8e0d-2a7a3cffec3b.png[/img]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。