"Spring and Springmvc" Automatically scans for annotation class issues

Source: Internet
Author: User

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:

    • <context:annotation-config/>   Its role is to implicitly register with the Spring container

      • Autowiredannota Tionbeanpostprocessor,
      • commonannotationbeanpostprocessor,
      •   Persistenceannotationbeanpostprocessor,
      • Requiredannotationbeanpostprocessor

        These 4 beanpostprocessor. The effect is that if you want to use annotations in your program, you must first register the corresponding class for the annotation, as shown in:

      • Dependent Classes-annotations:  
        • Commonannotationbeanpostprocessor @Resource, @PostConstruct, @PreDestroy
        • persistenceannotationbeanpostprocessor @PersistenceContext
        • autowiredannotationbeanpostprocessor @Autowired
        • requiredannotationbeanpostprocessor @Required

      Of course you can register yourself:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>  <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
    • 1
    • 2
  • <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.