7

In Spring 3.X does a class have to be annotated as component in order to Autowire a field into it?

Let's say I have:

@Service("myBean")
public class Mybean {

}

public class Target {
    @Autowired
    @Qualifier("myBean")
    private MyBean;
}

And then in my beans.xml

<context:component-scan base-package="com.package.beans" />

Where MyBean.java is within com.package.beans.MyBean package.

Does the class Target have to be annotated as @Component or some other spring annotation in order to autowire one of it's fields?

Adam Bronfin
  • 1,209
  • 3
  • 27
  • 43

1 Answers1

4

No, it doesn't have to be annotated, but it does have to be a Spring bean. You can do that by using a stereotype annotation such as @Component, but you can also make a Spring bean by declaring a <bean> element in your XML or by returning it from an @Bean configuration method.

Note that it is preferable to use constructor injection in any case, as it makes no difference in autowiring but makes testing much easier and makes certain mistakes more difficult.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • Even after annotating "Target" with Component("target"), it still autowires null, and I've set up a component scan on the package the class is in. In fact, other classes within the same package that have been autowiring it fine were also annotated with Component, but still no luck with "Target" after Component("target") annotation. Any idea what could be wrong? – Adam Bronfin Mar 31 '16 at 21:31
  • You're almost certainly calling `new Target()`. That's the "certain mistake" I mentioned. – chrylis -cautiouslyoptimistic- Mar 31 '16 at 23:01
  • A search across every project I have loaded into eclipse, shows not a single occurrence of "new Target()". This is not the problem. – Adam Bronfin Mar 31 '16 at 23:03
  • Switch to constructor injection anyway and retry. Spring *will not* "autowire null"; it'll throw an exception instead. – chrylis -cautiouslyoptimistic- Mar 31 '16 at 23:05
  • Why is Autowire not good? It doesn't make sense in this context for this class to have a constructor taking the field I'm attempting to autowire. And I don't think that's correct, I've on many occasions listed an autowired field only to see it as null, and realize I need to add a component scan. But in this case there's a component scan and it's still null. – Adam Bronfin Mar 31 '16 at 23:09
  • The reason I'd prefer not to do constructor injection is that this bean is not even explicitly defined in XML, it's just component annotated and registered via component scan. – Adam Bronfin Mar 31 '16 at 23:11
  • I didn't say not to autowire, I said to use constructor injection. Annotate the constructor with `@Autowired` or `@Inject` (or, in 4.3, have only one constructor), and it'll get wired. – chrylis -cautiouslyoptimistic- Mar 31 '16 at 23:12
  • Does the "Target" class still have to be annotated as a component? – Adam Bronfin Mar 31 '16 at 23:20
  • `@Component` is *only* if you're scanning for it--not if you're defining it in XML or creating it with `new` in an `@Bean` method. – chrylis -cautiouslyoptimistic- Mar 31 '16 at 23:27
  • What I'm asking is, you cannot wire a bean into any arbitrary class like a POJO without it itself being a bean, either via annotation or XML declaration? The class where the issue is coming from originally was NOT a bean, but even with Component annotation the problem persists. – Adam Bronfin Mar 31 '16 at 23:41
  • tl;dr: you cannot inject a spring bean into a POJO unless it is also a spring bean, otherwise how could spring knows that it needs to inject the proerty for you. spring will certainly scan all the classes for sicky annotations – Jaiwo99 Apr 01 '16 at 12:19
  • @Jaiwo99 Then why is added Component annotation not working? Also should mention, all of these issues are taking place in a project that's pulled in as a maven dependency in the main project. – Adam Bronfin Apr 01 '16 at 16:37
  • @AdamBronfin added annotation doesn't mean it is in the spring context, please check this. otherwise declare it in your config – Jaiwo99 Apr 01 '16 at 18:48
  • @Jaiwo99 I found out what was wrong and rephrased my question, please see http://stackoverflow.com/questions/36363387 – Adam Bronfin Apr 01 '16 at 18:50