(Turn) <context:component-scan> Instructions for Use

Reprinted from: http://blog.csdn.NET/chunqiuwei/article/details/16115135

        After configuring this tag in xml, spring can automatically scan the Java files under the base-pack or under the sub-package . If it scans the classes with annotations such as @Component @Controller@Service, it will register these classes as beans. (Will not scan AOP related solutions)

       Note: If <context:component-scan> is configured, then the <context:annotation-config/> tag can not be configured in xml, because the former includes the latter. In addition, <context:component-scan> also provides two subtags:

  • <context:include-filter>
  • <context:exclude-filter>

       Before explaining these two subtags, let's talk about <context:component-scan> has a use-default-filters attribute, and changing the attribute defaults to true, which means that all files marked with @Component under the specified package will be scanned. class, and registered as a bean. That is, the sub-annotation @Service, @Reposity, etc. of @Component. So if you just write this in the configuration file

<context:component-scan base-package="tv.huan.weisp.web"/>

       Use-default-filter is true at this time, then it will scan all java classes under the base-package package or sub-packages, and register the matching java classes as beans.

       It can be found that the granularity of this scan is a bit too large. What if you only want to scan the Controller under the specified package? At this point, the subtag <context:incluce-filter> comes into play. As follows

 

<context:component-scan base-package="tv.huan.weisp.web .controller">  
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   
</context:component-scan>  

 

       In this way, only the java classes under @Controller specified by base-package will be scanned and registered as beans, but because use-dafault-filter is not specified above, the default is true, so when the above configuration is changed to As shown below, it will produce results contrary to your expectations (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 annotated @Service under the subpackage service package where the specified package is located. At this time, the specified include-filter does not work, just set the use-default-filter to false is fine. In this way, you can avoid configuring multiple package names in base-packeage, which is not a very elegant way to solve this problem.

 

       In addition, in the projects I participated in, it can be found that some subpackages in the package specified by base-package do not contain annotations, so there is no need to scan. At this time, you can specify <context:exclude-filter> to filter, indicating that this package does not needs to be scanned. Summarize the above description.

       In the case of Use-dafault-filters=”false”: do not scan specified by <context:exclude-filter>, scan specified by <context:include-filter>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326309473&siteId=291194637