« | October 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9719995 建立时间:2004年12月20日 |

| |
[java语言]深入浅出 spring AOP (一) 原创空间, 软件技术
邢红瑞 发表于 2005/11/13 14:09:02 |
先不讨论AOP的各种名词,也不作各种AOP的比较,我将在例子中介绍各种名词。1。先写一个javabean,就是target object。package org.tatan.test;
public class MessageBean { public void write() { System.out.print("AOP example"); }}2。写一个AOP的advice类MethodInterceptor是AOP联盟的标准接口,它是最简单最实用的连接点(joinpoint),实现了around advice ,你可以在他返回前调用target的方法。package org.tatan.test;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class MessageCode implements MethodInterceptor {
public Object invoke(MethodInvocation invocation) throws Throwable { System.out.print("this is a "); Object returnValue = invocation.proceed(); return returnValue ; }
}3。把MessageCode advice weave 到proxy factory,proxy factory是整个架构的核心先创建instance of MessageBean,然后创建代理的instance ,MessageCode advice 传递给的addAdvice()方法,设置Target Object,调用getProxy()产生代理对象。import org.springframework.aop.framework.ProxyFactory;
public class AOPExample { public static void main(String[] args) { MessageBean target = new MessageBean(); ProxyFactory pf = new ProxyFactory(); pf.addAdvice(new MessageCode()); pf.setTarget(target); MessageBean proxy = (MessageBean) pf.getProxy(); //输出原始信息 target.write(); //输出代理对象调用的信息 proxy.write(); }
}4。classpath中加入cglib-nodep-2.1_2.jar ,spring.jar,aopalliance.jar,commons-logging.jar结果AOP examplethis is a AOP example |
|
回复:深入浅出 spring AOP (一) 原创空间, 软件技术
rong(游客)发表评论于2006/6/29 14:25:23 |
|
» 1 »
|