Spring automatically scans for conflicting issues with annotation classes

Source: Internet
Author: User

Original address: http://www.blogjava.net/crazycy/archive/2014/07/12/415738.html

The Spring MVC project typically has two profiles, Sprng-servlet.xml and applicationcontext.xml two profiles, typically with the following configurations

1. <context:annotation-config/>

Its role is to implicitly register with the Spring container
-Autowiredannotationbeanpostprocessor,
-Commonannotationbeanpostprocessor,
-Persistenceannotationbeanpostprocessor,
-Requiredannotationbeanpostprocessor of these 4 beanpostprocessor.

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

Dependent classes Annotations
Commonannotationbeanpostprocessor @Resource, @PostConstruct, @PreDestroy
Persistenceannotationbeanpostprocessor's Bean @PersistenceContext
Autowiredannotationbeanpostprocessor Bean @Autowired
Requiredannotationbeanpostprocessor @Required

Of course, you can also register yourself:

<bean class= "Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  



2. <context:component-scan base-package= "com.*" >

The <context:component-scan/> configuration item not only enables the ability to scan a class package to implement annotation-driven Bean definitions, but also enables annotation-driven auto-injection (that is, implicitly, internally registered Autowiredannotationbeanpostprocessor and Commonannotationbeanpostprocessor), so when using <context:component-scan/>, You can remove the <context:annotation-config/> from it.


One interesting question here is whether the scan needs to be configured in two configuration files, and I have done so several tests:

(1) Only configured as follows in Applicationcontext.xml

    <context:component-scan base-package= "Com.login"/>

Startup is normal, but no requests are intercepted, in short, @controller failure

(2) Configure the above configuration only in Spring-servlet.xml

Start normal, the request is normal, but the thing is invalid, that is, cannot roll back

(3) Configure the above information in both Applicationcontext.xml and Spring-servlet.xml

Start normal, request normal, also the thing is invalid, cannot roll back

(4) Configure the following in the Applicationcontext.xml

    <context:component-scan base-package= "Com.login"/>

In Spring-servlet.xml, configure the following

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


3. <mvc:annotation-driven/>

It will automatically register defaultannotationhandlermapping with Annotationmethodhandleradapter

4. Use-default-filters= "True" this means that all classes labeled @component under the specified package will be scanned and registered as beans. That is @component's child annotation @service, @Reposity, etc.

Source: http://www.cnblogs.com/fangqi/archive/2012/12/11/2812745.html

Spring automatically scans for conflicting issues with annotation classes

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.