Is it possible to autowire beans using the @Autowired
annotation without using component scanning?
So the answer is: No, @Autowired does not necessarily mean you must also use @Component . It may be registered with applicationContext. xml or @Configuration+@Bean .
Thanks. I am sure you cannot Autowire beans which are not managed by Spring.. You need to get hold of the reference of an instance of XYZ by some other mean. If it is a Helper class make the methods of XYZ static and use them using the class name.
With Spring, we use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.
Yes. <context-component-scan .. />
is responsible for discovering beans annotated with @Component
, @Controller
, @Service
, @Respository
, etc.
In order to have annotations processed (@Autowired
, @Resource
, etc) you need <context:annotation-config />
. Thus annotations are processed on beans that are listed in applicationContext.xml
.
As far as I know, <context-component-scan .. />
activates <context:annotation-config />
automatically.
This is true for both spring 2.5 and 3.0. (thanks skaffman)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With