This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

AnnouncementAnnouncement Module

Collapse

No announcement yet.

@Autowired not working for custom @Component annotationPage Title Module

@Autowired not working for custom @Component annotation

Jul 20th, 2009, 04:23 PM

I have created a custom annotation called @Coordinator that is itself annotated with Spring's @Component annotation. My classes annotated with @Coordinator are not being autowired. According to the docs:

"By default, classes annotated with @Component, @Repository, @Service, or @Controller (or classes annotated with a custom annotation that itself is annotated with @Component) are the only detected candidate components."

In accordance with the docs, my SearchCoordinator class is "annotated with a custom annotation that itself is annotated with @Component." However, my SearchService instance never gets autowired. FYI, SearchService itself contains autowired daos which DO get autowired as one would expect, but then again, SearchService is annotated with @Service, not my custom annotation.

Furthermore, if I try to annotate my class with BOTH the @Coordinator and @Component annotations (and take the @Component annotation OUT of @Coordinator,) it still does not work. However, if I use ONLY the standard @Component annotation, everything works fine. I've been trying to trace this in the code at startup, but I can't even find the place that the implicitly declared AutowiredAnnotationBeanPostProcessor is ever called. (I am under the impression that this post processor is responsible for doing the autowiring.)

I suspect this has to do with AnnotationConfigUtils and it's behavior of walking up the inheritance stack. Perhaps Spring is not "seeing" the @Component annotation on your @Coordinator annotation because it is not looking at whether your annotation inherits from @Component.

Comment

Hmm... jamestastic, yes, I see where AnnotationConfigUtils sets up the proper bean post processor now. Gonna trace down from there and see what I can see.

Denis, something else I never mentioned that may or may not be a problem, but my @Coordinator annotated class is in a different jar from the one that has the main context. I wouldn't think this would be a problem, but I'm going to try moving it to see what happens. Good to know it works for you, though. Now I know it is indeed supposed to work that way. This probably means I've wired something incorrectly and just haven't seen it yet. Will report back with details.

Comment

In AbstractApplicationContext, my @Coordinator annotated bean is being loaded and initialized during the invokeBeanFactoryPostProcessors() phase. This happens before the AutowireAnnotationBeanPostProcessor is registered. Other beans that require AutowireAnnotationBeanPostProcessor are not being registered until the call to finishBeanFactoryInitialization() which comes after invokeBeanFactoryPostProcessors(). Now, I have to figure out why this one particular @Coordinator bean is being initialized and cached (and therefore skipped during the post processing of the other beans) a few steps too early. Gotta be a pebkac error in here somewhere...

Thanks for the help so far, everyone.

Comment

Found it. I had a rogue BeanFactoryPostProcessor that was itself loading all @Coordinators. Because of that, the @Coordinators were being cached and never initialized by any of the default BeanPostProcessors (other than ApplicationAware and ServletAware.) Woops.