Respect original source Link: http://blog.csdn.net/u014277445/article/details/52282697
The Spring MVC project typically has two configuration files, Spring-servlet.xml and applicationcontext.xml two profiles, typically with the following configurations:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
<context:component-scan base-package="com.*" >
<context:component-scan/>
Configuration items not only enable the ability to scan class packages to implement annotation-driven Bean definitions, but also enable annotation-driven auto-injection (that is, implicitly, internally registered Autowiredannotationbeanpostprocessor and Commonannotationbeanpostprocessor), so when used <context:component-scan/>
, it can be <context:annotation-config/>
removed.
One interesting question here is whether the scan needs to be configured in two configuration files, and I have done so several tests:
This is only configured in Applicationcontext.xml
<context:component-scan base-package="com.login" />
Startup is normal, but no requests are intercepted, in short, @controller failure
Configuring the above configuration only in Spring-servlet.xml is normal, the request is normal, but the thing fails, that is, it cannot be rolled back.
Configure the above information in both Applicationcontext.xml and Spring-servlet.xml
Start normal, request normal, also the thing is invalid, cannot roll back
In Applicationcontext.xml, configure the following in <context:component-scan base-package="com.login" />
Spring-servlet.xml:
<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). You can also use Context:include-filter and context:exclude-filter to filter annotations that do not require scanning.
<mvc:annotation-driven />
It will automatically register defaultannotationhandlermapping with Annotationmethodhandleradapter
Spring and SPRINGMVC automatically scans the annotation class for problems