Spring <context:component-scan> (GO)

Source: Internet
Author: User

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)

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.