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
- <context:component-scan base-package="Org.bdp.system.test.controller" >
- <context:include-filter type="annotation" expression="Org.springframework.stereotype.Controller"/>
- </context:component-scan>
But the following way, not only scanning @controller, but also scanning @service/@Repository beans, may cause some problems
Java code
- <context:component-scan base-package="ORG.BDP" >
- <context:include-filter type="annotation" expression="Org.springframework.stereotype.Controller"/>
- </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
- 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
- protected void Registerdefaultfilters () {
- This.includeFilters.add (new Annotationtypefilter (Component. Class));
- ClassLoader cl = Classpathscanningcandidatecomponentprovider. Class.getclassloader ();
- try {
- This.includeFilters.add (new Annotationtypefilter (
- (CLASS<? extends Annotation>) cl.loadclass ("Javax.annotation.ManagedBean"), false));
- Logger.info ("JSR-250 ' Javax.annotation.ManagedBean ' found and supported for component scanning");
- }
- catch (ClassNotFoundException ex) {
- //JSR-250 1.1 API (as included in Java EE 6) is not available-simply skip.
- }
- try {
- This.includeFilters.add (new Annotationtypefilter (
- (CLASS<? extends Annotation>) cl.loadclass ("javax.inject.Named"), false));
- Logger.info ("JSR-330 ' javax.inject.Named ' annotation found and supported for component scanning");
- }
- catch (ClassNotFoundException ex) {
- //JSR-330 API not available-simply skip.
- }
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
- Protected Boolean iscandidatecomponent (Metadatareader metadatareader) throws IOException {
- For (TypeFilter TF: this.excludefilters) {
- if (Tf.match (Metadatareader, this.metadatareaderfactory)) {
- return false;
- }
- }
- For (TypeFilter TF: this.includefilters) {
- if (Tf.match (Metadatareader, this.metadatareaderfactory)) {
- Annotationmetadata metadata = Metadatareader.getannotationmetadata ();
- if (!metadata.isannotated. Class.getname ())) {
- return true;
- }
- Annotationattributes profile = metadatautils.attributesfor (metadata, profile. Class);
- return this.environment.acceptsProfiles (Profile.getstringarray ("value"));
- }
- }
- return false;
- }
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
- <context:component-scan base-package="ORG.BDP" >
- <context:include-filter type="annotation" expression="Org.springframework.stereotype.Controller"/>
- </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