Tags

, , ,

A few days ago I saw a question in stackoverflow(http://bit.ly/11k6hp1) asking the difference between <context:annotation-config> and <context:component-scan> in Spring MVC 3.0. I think this will be asked in technical interviews against your Spring skills.

The answer is

<context:annotation-config> is used to activate annotations in beans already registered in the application context (no matter if they were defined with XML or by package scanning).

<context:component-scan> can also do what <context:annotation-config> does but <context:component-scan> also scans packages to find and register beans within the application context.

I found this nice summary of which annotations are picked up by which declarations (http://bit.ly/Y3RWAt).

By studying it you will find that <context:component-scan/> recognizes a super-set of annotations recognized by <context:annotation-config/>, namely:

@Component, @Service, @Repository, @Controller, @Endpoint, @Configuration, @Bean, @Lazy, @Scope, @Order, @Primary, @Profile, @DependsOn, @Import, @ImportResource
As you can see <context:component-scan/> logically extends <context:annotation-config/> with CLASSPATH component scanning and Java @Configuration features

Advertisements