【发布时间】:2012-03-26 21:23:12
【问题描述】:
我有以下弹簧配置:
<context:component-scan base-package="uk.co.mysite.googlecontactsync.aop"/>
<bean name="simpleEmailSender" class="uk.co.mysite.util.email.simple.SimpleEmailSenderImplementation"/>
<aop:aspectj-autoproxy/>
那我有一个方面:
@Aspect
public class SyncLoggingAspect {
@Autowired
private SimpleEmailSender simpleEmailSender
@AfterReturning(value="execution(* uk.co.mysite.datasync.polling.Poller+.doPoll())", returning="pusher")
public void afterPoll(Pusher pusher) {
simpleEmailSender.send(new PusherEmail(pusher));
}
}
这方面有效(我可以在 afterPoll 上打断点)但 simpleEmailSender 为空。不幸的是,我找不到关于为什么会这样的明确文档。 (作为记录,我的 simpleEmailSender bean 存在并正确连接到其他类)以下事情让我感到困惑:
- context:component-scan 是否应该选择@Aspect?如果是,那么它肯定是一个 spring 管理的 bean,因此 autowired 应该可以工作吗?
- 如果 context:component-scan 不是用于创建方面,我的方面是如何创建的?我以为 aop:aspectj-autoproxy 只是创建一个 beanPostProcessor 来代理我的 @Aspect 类?如果它不是 Spring 托管的 bean,它会如何做到这一点?
显然,您可以说我不了解事情应该如何从头开始。
【问题讨论】:
-
你能在
@Aspect旁边加@Service吗? -
它仍然为空,但我现在收到 INFO : Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@76faf7d6: defined beans [..., SyncLoggingAspect in the logs
-
setter 注入或注释私有字段应该没有区别
-
是否有可能
必须存在于应用程序上下文中才能检测@Autowired 注释? -
@kclaes 和
context:component-scan没有必要,因为这也会注册AutowiredAnnotationBeanPostProcessor。Furthermore, the AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor are both included implicitly when you use the component-scan element.-- 来自static.springsource.org/spring/docs/3.0.x/…
标签: java spring aop aspectj spring-aop