Announcement Announcement Module
Collapse
No announcement yet.
@Autowired not working for custom @Component annotation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowired not working for custom @Component annotation

    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."

    Here is some sample code. First my custom @Component annotation:

    Code:
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public @interface Coordinator  {
    	String value() default "";
    	...
    }
    Here's a class it annotates:

    Code:
    @Coordinator
    public class SearchCoordinator {
    
    	@Autowired
    	private SearchService searchService;
    
    	...
    }
    Of course, my context.xml file has:

    Code:
    <context:component-scan base-package="x.y.z" />
    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'm using Spring 2.5.6.SEC01. Any ideas?

  • #2
    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.

    I have encountered this before when I was working on the @Remote annotation (http://jira.springframework.org/browse/SPR-3926). Check out AnnotationConfigUtils and see if it leads you anywhere.

    Comment


    • #3
      Originally posted by mcsenter View Post
      ...

      I'm using Spring 2.5.6.SEC01. Any ideas?
      Your example works fine for me, i.e. @Coordinator-marked bean is correctly picked up and its @Autowired dependencies are set.

      Comment


      • #4
        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


        • #5
          Ok, I've narrowed it down to this:

          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


          • #6
            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.

            Comment

            Working...
            X