Sunday, December 23, 2012

context:component-scan, context:annotation-config and list of beans registered

A <context:component-scan/> custom tag registers the same set of bean definitions as is done by <context:annotation-config/>, apart from its primary responsibility of scanning the java packages and registering bean definitions from the classpath.

The following bean types are registered by either of these custom tags:

Bean Purpose
A bean factory post processor used for bootstrapping classes annotated with @Configuration
A bean post processor which processes fields, setter methods, arbitrary methods annotated with @AutoWired, @Value and supports JSR-330 @Inject annotation
A bean post processor which enforces that the required bean properties have been set - by looking for fields annotated with @Required annotation
A bean post processor which supports JSR-250 annotations - eg. @Resource
A bean post processor that processes @PersistenceUnit and @PersistenceContext annotations for injecting in a EntityManagerFactory and EntityManager respectively.

This is the reason why if component-scan is present, annotation-config can be kept out.

If for some reason this registration of default bean definitions are to be avoided, the way to do that is to specify an additional "annotation-config" attribute in component-scan, this way:

<context:component-scan basePackages=".." annotation-config="false"/>

Spring Reference site

No comments:

Post a Comment