SpringMVC configured differentiator context: annotation-config, context: component-scan, mvc: annotation-driven

SpringMVC xml configuration minutiae

In springMVC configuration, some annotations configured to process corresponding to, the following points are more difficult to distinguish the configuration:

  1. <context:annotation-config />
  2. <context:component-scan base-package=“com.xx.xx” />
  3. <mvc:annotation-driven />



<context:annotation-config />

  1. effect

    Disposed in the Spring ** <context: annotation-config / > **, its role is to implicitly register the following Spring container processors: the AutowiredAnnotationBeanPostProcessor

    CommonAnnotationBeanPostProcessor

    PersistenceAnnotationBeanPostProcessor

    RequiredAnnotationBeanPostProcessor

  2. 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"/>
  1. 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"/>
  1. 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"/>
  1. 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” />

  1. 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.

  2. 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 .

  3. Support notes

    1. @Autowired
    2. @Component
    3. @Repository
    4. @Service
    5. @Controller
    6. @RestController,
    7. @ControllerAdvice,
    8. @Configuration



<mvc:annotation-driven />

  1. effect

    For start @Controller comment

  2. 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.

Published 11 original articles · won praise 0 · Views 47

Guess you like

Origin blog.csdn.net/DavinDeng/article/details/104919477