日期:2014-05-17  浏览次数:20667 次

请教Spring AOP问题
/*
当我在执行 service.add(new User());会被spring拦截并在方法执行前执行before方法,我尝试去掉@Component,然后直接new一个LogInterceptor类,但这样就不成功了,请问自己new一个类和用Spring工厂管理有什么区别?
*/
@Aspect
@Component
public class LogInterceptor {

@Before("execution(public void com.bjsxt.dao.UserDAOImpl.save(com.bjsxt.model.User))")
public void before(){
  Sysout.out.println("this is before method!");
  }
}

public class Test{
  ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  UserManager service = (UserManager) ctx.getBean("UserManager");
/*
假如去掉@Component,而使用LogInterceptor log = new LogInterceptor();创建对象就执行不成功,为什么呢?
*/
  service.add(new User());
}

------解决方案--------------------
大哥你真懒。连main方法都不写。spring不是有控制反转吗。用反转啊。你怎么还在用构造器来创建javabean,反转一个javabean.从ioc容器中取出来不就得了。纠结这样的问题没啥意思啊。