浏览 900 次
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-02-22
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); Waiter w=(Waiter)ctx.getBean("waiter"); w.greetto("yinfan"); XML: <aop:aspectj-autoproxy/> <bean id="waiter" class="aspect.implewaiter"></bean> <bean class="aspect.pregreetingaspect"/> 目标类: package aspect; public class implewaiter implements Waiter { public void greetto(String name) { // TODO Auto-generated method stub System.out.println("greet to "+name); } public void serveto(String name) { // TODO Auto-generated method stub System.out.println("server to "+name); } } 切面代码: package aspect; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class pregreetingaspect { @Before("execution(* greetto(..))") public void before() { System.out.println("how are you "); } } 结果: how are you how are you greet to yinfan 你看,有两个how are you 呢? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-02-22
感觉是annotation已经起了将aspect加上的作用了。
要么在applicationContext.xml里定义bean pregreetingaspect 要么在代码中使用@Aspect 两者取一即可. 自己跑程序验证了下,楼主没有问题,只输出了一个how are you. btw 用的是spring2.5的库. 建议楼主升级下spring。 |
|
返回顶楼 | |