Spring MVC Annotation Method Scan order for service and controller

Source: Internet
Author: User

Reprint: http://blog.csdn.net/xwnxwn/article/details/52679578

Ways to get Webapplicationcontext: http://panyongzheng.iteye.com/admin/blogs/1477702

XML is scanned in sequence reference: http://sence-qi.iteye.com/blog/1328902

The bean that @controller annotations can be successfully scanned in the following way, not the bean that the @service/@Repository is scanned. That's right

Java code
    1. <context:component-scan base-package="Org.bdp.system.test.controller" >
    2. <context:include-filter type="annotation" expression="Org.springframework.stereotype.Controller"/>
    3. </context:component-scan>

But the following way, not only scanning @controller, but also scanning @service/@Repository beans, may cause some problems

Java code
    1. <context:component-scan base-package="ORG.BDP" >
    2. <context:include-filter type="annotation" expression="Org.springframework.stereotype.Controller"/>
    3. </context:component-scan>

This is especially true in the case of integration with Springmvc+spring+hibernate, the most typical error is:

Transaction does not work

What's the problem?

Analysis

1, <context:component-scan> will be handed over to org.springframework.context.config.ContextNamespaceHandler processing;

Java code
    1. Registerbeandefinitionparser ("Component-scan", new Componentscanbeandefinitionparser ());

2. Componentscanbeandefinitionparser will read the configuration file information and assemble it into org.springframework.context.annotation.ClassPathBeanDefinitionScanner for processing;

3. If there is no Use-default-filters property configured <context:component-scan>, the default is true, The following code is called when the Classpathbeandefinitionscanner is created, depending on whether Use-default-filters is true:

Java code
  1. protected void Registerdefaultfilters () {
  2. This.includeFilters.add (new Annotationtypefilter (Component.   Class));
  3. ClassLoader cl = Classpathscanningcandidatecomponentprovider.  Class.getclassloader ();
  4. try {
  5. This.includeFilters.add (new Annotationtypefilter (
  6. (CLASS<?  extends Annotation>) cl.loadclass ("Javax.annotation.ManagedBean"), false));
  7. Logger.info ("JSR-250 ' Javax.annotation.ManagedBean ' found and supported for component scanning");
  8. }
  9. catch (ClassNotFoundException ex) {
  10. //JSR-250 1.1 API (as included in Java EE 6) is not available-simply skip.
  11. }
  12. try {
  13. This.includeFilters.add (new Annotationtypefilter (
  14. (CLASS<?  extends Annotation>) cl.loadclass ("javax.inject.Named"), false));
  15. Logger.info ("JSR-330 ' javax.inject.Named ' annotation found and supported for component scanning");
  16. }
  17. catch (ClassNotFoundException ex) {
  18. //JSR-330 API not available-simply skip.
  19. }

Both the @Service and @controller are component, because these annotations add @component annotations

You can see that the default Classpathbeandefinitionscanner automatically registers @component, @ManagedBean, @Named annotated beans for scanning. If we are careful, we will find the source of the problem.

4, in the scan will be include-filter/exclude-filter to determine whether your bean class is legitimate:

Java code
  1. Protected Boolean iscandidatecomponent (Metadatareader metadatareader) throws IOException {
  2. For (TypeFilter TF: this.excludefilters) {
  3. if (Tf.match (Metadatareader, this.metadatareaderfactory)) {
  4. return false;
  5. }
  6. }
  7. For (TypeFilter TF: this.includefilters) {
  8. if (Tf.match (Metadatareader, this.metadatareaderfactory)) {
  9. Annotationmetadata metadata = Metadatareader.getannotationmetadata ();
  10. if (!metadata.isannotated. Class.getname ())) {
  11. return true;
  12. }
  13. Annotationattributes profile = metadatautils.attributesfor (metadata, profile.   Class);
  14. return this.environment.acceptsProfiles (Profile.getstringarray ("value"));
  15. }
  16. }
  17. return false;
  18. }

The default scan specifies that all @Component under the package, Exclude-filter specifies that no scans, Include-filter specified scans, Include-filter, and Exclude-filter are still scanned that are not specified. To

Use-default-filters= "false" is specified.
First, the blacklist is filtered by exclude-filter;

Then through the Include-filter white list filter;

Otherwise, the default exclusion

Conclusion Java code
    1. <context:component-scan base-package="ORG.BDP" >
    2. <context:include-filter type="annotation" expression="Org.springframework.stereotype.Controller"/>
    3. </context:component-scan>

Why this code not only scans @controller annotated beans, but also scans the @component sub-annotations @service, @Reposity. Because use-default-filters defaults to true. So if the default is not required, use-default-filters= "false" is disabled.

Spring MVC Annotation Method Scan order for service and controller

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.