SpringMVC xml configuration minutiae
In springMVC configuration, some annotations configured to process corresponding to, the following points are more difficult to distinguish the configuration:
- <context:annotation-config />
- <context:component-scan base-package=“com.xx.xx” />
- <mvc:annotation-driven />
<context:annotation-config />
-
effect
Disposed in the Spring ** <context: annotation-config / > **, its role is to implicitly register the following Spring container processors: the AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
-
AutowiredAnnotationBeanPostProcessor
This processor is mainly to be registered as ** @ Autowired ** annotation, if you need to use the notes need to be registered
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
-
CommonAnnotationBeanPostProcessor
This processor is mainly to be registered as ** @ PersistenceContext ** annotation, if you need to use the notes need to be registered
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
-
PersistenceAnnotationBeanPostProcessor
This processor is mainly to be registered as ** @ Required ** annotation, if you need to use the notes need to be registered
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
-
RequiredAnnotationBeanPostProcessor
This processor is mainly to be registered as ** @ Resource, @ PostConstruct, @ PreDestroy etc. ** Notes, if required by the comment you need to Register
<bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>
<context: annotation-config /> only has registered a bean to work, that is to say, those described above can not be used in the notes has not been registered with the bean.
<context:component-scan base-package=“com.xx.xx” />
-
effect
Effect of this configuration is to start the annotation bean definition , that is, the scan on the definition of the class notes in base-package specified in the package, so you do not have a traditional bean to register.
-
important point
NOTE: <context: component-scan base -package = "com.xx.xx" /> only enabled for scan-based package to drive annotation embodiment Bean defined function, but also enables annotation-driven automatic injector features (i.e. also implicitly registered inside and AutowiredAnnotationBeanPostProcessor CommonAnnotationBeanPostProcessor), so when using
<context:component-scan/>
the, can be<context:annotation-config/>
removed . -
Support notes
- @Autowired
- @Component
- @Repository
- @Service
- @Controller
- @RestController,
- @ControllerAdvice,
- @Configuration
<mvc:annotation-driven />
-
effect
For start @Controller comment
-
important point
To use @Controller annotations in spring mvc, it is necessary to configure the <MVC: Annotation-Driven /> , otherwise org.springframework.web.servlet.DispatcherServlet unable to find the controller and distribute the request to the controller.