java - Spring autowired bean for @Aspect aspect is null -
i have following spring configuration:
<context:component-scan base-package="uk.co.mysite.googlecontactsync.aop"/> <bean name="simpleemailsender" class="uk.co.mysite.util.email.simple.simpleemailsenderimplementation"/> <aop:aspectj-autoproxy/>
then have aspect:
@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)); } }
this aspect works (i can hit breakpoint on afterpoll) simpleemailsender null. unfortunately cannot find clear documentation on why is. (for record, simpleemailsender bean exists , correctly wired other classes) following things confuse me:
- is context:component-scan supposed picking @aspect? if surely spring managed bean, autowired should work?
- if context:component-scan isn't creating aspects, how aspect being created? thought aop:aspectj-autoproxy creates beanpostprocessor proxy @aspect class? how if isn't spring managed bean?
obviously can tell don't have understanding of how things should working ground up.
the aspect singleton object , created outside spring container. solution xml configuration use spring's factory method retrieve aspect.
<bean id="syncloggingaspect" class="uk.co.demo.syncloggingaspect" factory-method="aspectof" />
with configuration aspect treated other spring bean , autowiring work normal.
you have use factory-method on enum objects , other objects without constructor or objects created outside spring container.
Comments
Post a Comment