欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

annotation-config vs(对比) component-scan

程序员文章站 2022-04-05 22:05:51
...
<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/>

http://techidiocy.com/annotation-config-vs-component-scan-spring-core/

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.