<context:annotation-config>
Only resolves the @Autowired
and @Qualifer
annotations, that's all, it about the Dependency Injection, There are other annotations that do the same job, I think how @Inject
, but all about to resolve DI through annotations.
Be aware, even when you have declared the <context:annotation-config>
element, you must declare your class how a Bean anyway, remember we have three available options
<bean>
Now with
<context:component-scan>
It does two things:
<context:annotation-config>
does.Therefore if you declare <context:component-scan>
, is not necessary anymore declare <context:annotation-config>
too.
Thats all
A common scenario was for example declare only a bean through XML and resolve the DI through annotations, for example
<bean id="serviceBeanA" class="com.something.CarServiceImpl" />
<bean id="serviceBeanB" class="com.something.PersonServiceImpl" />
<bean id="repositoryBeanA" class="com.something.CarRepository" />
<bean id="repositoryBeanB" class="com.something.PersonRepository" />
We have only declared the beans, nothing about <constructor-arg>
and <property>
, the DI is configured in their own classes through @Autowired. It means the Services use @Autowired for their Repositories components and the Repositories use @Autowired for the JdbcTemplate, DataSource etc..components