After the XML is configured with this tag, spring can automatically scan the Java files under Base-pack or under the sub-packages, and if scanned into classes with annotations such as @component @[email protected], register these classes as beans
Note: If <context:component-scan> is configured, the <context:annotation-config/> tag can be configured without XML, because the former contains the latter. The <context:annotation-config/> also provides two sub-labels
1. <context:include-filter>
2. <context:exclude-filter>
Before you explain these two sub-labels, say the <context:component-scan> has a Use-default-filters property, change the property by default to True, This means that all of the @component-labeled classes under the specified package are scanned and registered as beans. That is @component @service, @Reposity, etc. So if it's just written in the config file,
<context:component-scan base-package="Tv.huan.weisp.web"/>
Use-default-filter This is true then all the Java classes under the Base-package package or sub-package are scanned, and the matching Java class is registered as a bean.
You can see that the granularity of this scan is a bit too large, what if you just want to scan the controller under the specified package? At this point, the sub-label <context:incluce-filter> will play a chivalrous place. As shown below
<context:component-scan base-package= "Tv.huan.weisp.web. Controller" >
<context:include-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/>
</context:component-scan>
This will only scan the Java class under the base-package specified under @controller, and register as a bean
But because Use-dafault-filter is not specified above, the default is true, so when the above configuration is changed to the following, it will produce a result that is contrary to your expectation (note that the Base-package package is worth changing)
<context:component-scan base-package= "Tv.huan.weisp.web" >
<context:include-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/>
</context:component-scan>
At this point, spring not only scans the @controller, but also scans the Java class for annotation @service under the Child package service package where the specified package resides
The specified Include-filter does not work at this time, as long as the Use-default-filter is set to false. This avoids the elegant approach of configuring multiple package names in Base-packeage to solve this problem.
In addition, I can find in the project that the base-package specified in the package contains no annotations, so do not scan, you can specify <context:exclude-filter> to filter, that the package does not need to be scanned. Integrated above instructions
Use-dafault-filters= "false" in case:<context:exclude-filter> specified does not scan,<context:include-filter> specified scan
Spring <context:component-scan> (GO)