Original address: http://www.blogjava.net/crazycy/archive/2014/07/12/415738.html
The Spring MVC project typically has two profiles, Sprng-servlet.xml and applicationcontext.xml two profiles, typically with the following configurations
1. <context:annotation-config/>
Its role is to implicitly register with the Spring container
-Autowiredannotationbeanpostprocessor,
-Commonannotationbeanpostprocessor,
-Persistenceannotationbeanpostprocessor,
-Requiredannotationbeanpostprocessor of these 4 beanpostprocessor.
The effect is that if you want to use annotations in your program, you must first register the corresponding class for that annotation, as shown in:
Dependent classes |
Annotations |
Commonannotationbeanpostprocessor |
@Resource, @PostConstruct, @PreDestroy |
Persistenceannotationbeanpostprocessor's Bean |
@PersistenceContext |
Autowiredannotationbeanpostprocessor Bean |
@Autowired |
Requiredannotationbeanpostprocessor |
@Required |
Of course, you can also register yourself:
<bean class= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
2. <context:component-scan base-package= "com.*" >
The <context:component-scan/> configuration item not only enables the ability to scan a class package to implement annotation-driven Bean definitions, but also enables annotation-driven auto-injection (that is, implicitly, internally registered Autowiredannotationbeanpostprocessor and Commonannotationbeanpostprocessor), so when using <context:component-scan/>, You can remove the <context:annotation-config/> from it.
One interesting question here is whether the scan needs to be configured in two configuration files, and I have done so several tests:
(1) Only configured as follows in Applicationcontext.xml
<context:component-scan base-package= "Com.login"/>
Startup is normal, but no requests are intercepted, in short, @controller failure
(2) Configure the above configuration only in Spring-servlet.xml
Start normal, the request is normal, but the thing is invalid, that is, cannot roll back
(3) Configure the above information in both Applicationcontext.xml and Spring-servlet.xml
Start normal, request normal, also the thing is invalid, cannot roll back
(4) Configure the following in the Applicationcontext.xml
<context:component-scan base-package= "Com.login"/>
In Spring-servlet.xml, configure the following
<context:component-scan base-package= "Com.sohu.login.web"/>
The start is normal, the request is normal, the things are normal.
Conclusion: In Spring-servlet.xml, you only need to scan all classes with @controller annotations, and in ApplicationContext you can scan all other annotated classes (you can also filter out classes with @controller annotations).
3. <mvc:annotation-driven/>
It will automatically register defaultannotationhandlermapping with Annotationmethodhandleradapter
4. Use-default-filters= "True" this means that all classes labeled @component under the specified package will be scanned and registered as beans. That is @component's child annotation @service, @Reposity, etc.
Source: http://www.cnblogs.com/fangqi/archive/2012/12/11/2812745.html
Spring automatically scans for conflicting issues with annotation classes