哪个高手帮菜鸟看哈这个有关问题嘛~小弟已经困惑几天了~
哪个高手帮初学者看哈这个问题嘛~~~小弟已经困惑几天了~~~~~~~
主函数:
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 呢?
主函数:
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 呢?
1 楼
dwangel
2008-02-22
感觉是annotation已经起了将aspect加上的作用了。
要么在applicationContext.xml里定义bean pregreetingaspect
要么在代码中使用@Aspect
两者取一即可.
自己跑程序验证了下,楼主没有问题,只输出了一个how are you.
btw 用的是spring2.5的库.
建议楼主升级下spring。
要么在applicationContext.xml里定义bean pregreetingaspect
要么在代码中使用@Aspect
两者取一即可.
自己跑程序验证了下,楼主没有问题,只输出了一个how are you.
btw 用的是spring2.5的库.
建议楼主升级下spring。