<mvc:annotation-driven />, <context:annotation-config/>, <context:component-scan

<mvc:annotation-driven />  will do the following:  

  1. Register DefaultAnnotationHandlerMapping with the spring container.
  2. Register the AnnotationMethodHandlerAdapter with the spring container.
  3. Configure some messageconverters.
  4. Resolved the premise configuration of the @Controller annotation, that is, HandlerMapping can know who handles the request.

<context:annotation-config />  will do the following:  

  1. Register AutowiredAnnotationBeanPostProcessor with the spring container.
  2. Register CommonAnnotationBeanPostProcessor with the spring container.
  3. Register PersistenceAnnotationBeanPostProcessor with the spring container.
  4. Register RequiredAnnotationBeanPostProcessor with the spring container.
  5. Before using <context:annotationconfig />, the context namespace <context:component-scan /> must be declared in the <beans> element. <context:component-scan /> scans the package and implements annotation-driven bean definitions. That is, a bean of the class identified by @Controller is registered with the container.

<context:component-scan/>

Not only enables the scanning of class packages to implement annotation-driven bean definitions, but also enables annotation-driven automatic injection (that is, AutowiredAnnotationBeanPostProcessor and CommonAnnotationBeanPostProcessor are implicitly registered internally). Therefore, when using <context:component-scan />, you can remove <context:annotation-config /> unless you need to use the functions of PersistenceAnnotationBeanPostProcessor and RequiredAnnotationBeanPostProcessor (such as JPA, etc.).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326939889&siteId=291194637