[size=small]<cx:annotation-config/> 可以处理@Autowired,但是不能处理@Component, @Service, @Repository 所以如果你使用了@Component, @Service, @Repository就必须添加<cx:component-scan base-package="">.
<cx:component-scan ..>包含了<cx:annotation-config/>的功能,并且还可以处理@Component, @Service, @Repository, 所以你只要使用了<cx:component-scan ..>,
就不需要添加<cx:annotation-config/>
[url]http://techidiocy.com/annotation-config-vs-component-scan-spring-core/[/url]
annotation-config : Annotation config main job is to activate all the annotations that are present in java beans and those are already registered either by defining in your application context file or being registered while component scanning. Important point is they need to be registered.
component-scan : Component scan can do everything that annotation config does , in addition to it it also registers the java classes as spring bean those are annotated with @Component , @Service ,@Repository etc.[/size]
<cx:component-scan ..>包含了<cx:annotation-config/>的功能,并且还可以处理@Component, @Service, @Repository, 所以你只要使用了<cx:component-scan ..>,
就不需要添加<cx:annotation-config/>
[url]http://techidiocy.com/annotation-config-vs-component-scan-spring-core/[/url]
annotation-config : Annotation config main job is to activate all the annotations that are present in java beans and those are already registered either by defining in your application context file or being registered while component scanning. Important point is they need to be registered.
component-scan : Component scan can do everything that annotation config does , in addition to it it also registers the java classes as spring bean those are annotated with @Component , @Service ,@Repository etc.[/size]
本文对比了Spring框架中<cx:annotation-config>和<cx:component-scan>的区别与联系。前者主要用于激活Java Bean中的注解,后者不仅具备前者功能,还能将被@Component、@Service、@Repository等注解标记的Java类注册为Spring Bean。
272

被折叠的 条评论
为什么被折叠?



