spring annotation injection: <context: component-scan> and where the context: include-filter> and <context: exclude-filter> is doing? injection spring annotation: <context: component-scan> Description

Transfer: https://www.cnblogs.com/vanl/p/5733655.html

 

injection spring annotation: <context: component-scan> Description

 

  From the start of spring version 2.5 supports annotation injection, injection annotations can save a lot of xml configuration. Since the annotations are written in java code, so the injection annotations will lose some flexibility, we have to choose whether to enable annotation injected as needed.

  After configuring the tag in XML, spring base-package can automatically scan the following sub-packets or following java file, if the scanning has @Component @Service @Controller like those annotated classes, these classes put registered as bean.

  Note: If you configure the <context: component-scan> then <context: annotation-config /> tag can no longer xml configuration because the former includes the latter. Further <context: annotation-config /> tag also provides two sub

  1. <context:include-filter>

  2. <context:exclude-filter>

  In the description of these two sub-label before, first talk about <context: component-scan> have a use-default-filters property, change the default attribute is true, which means all of the standard scans under specified package annotated class and registered as a bean so if only so written in the configuration file. <context: the Component-Scan base-Package Penalty for = "tv.huan.weisp.web" /> the Use-default-Filters is true that at this time will base All were under the java class -package packet or sub-packet scans, and the class is registered into the matching java bean.

  This scan can be found granularity bit too much, if you want to specify the following Controller scan package, how to do? At this time, the sub-label <context: incluce-filter> valor serves as a ground. 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>  

  This will only scan the specified base-package are under the java class @Controller, and registered into the bean. But because the use-dafault-filters and not specified in the above, it is by default true, so when the above configuration is changed as shown below and they will produce (note change in base-package package worth) and you expect inconsistent results

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

    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   

  </context:component-scan>  

  At this time, spring @Controller only scanned, the scanning further java classes specified subpackages of the package where the package annotations service @Service this time is not specified functions include-filter, as long as the use-default-filters arranged false on it. This can be avoided in the base-packeage configure multiple package names this is not a very elegant way to solve this problem.

Also in the project I was involved in base-package can be found in the package specified in sub-package does not contain any notes, so do not scan, then you can specify <context: exclude-filter> to filter, this package does not You need to be scanned. Based on the above description

  Case Use-dafault-filters = "false" is: <context: exclude-filter> is not specified scan, <context: include-filter> specified scan

  From the start of spring version 2.5 supports annotation injection, injection annotations can save a lot of xml configuration. Since the annotations are written in java code, so the injection annotations will lose some flexibility, we have to choose whether to enable annotation injected as needed.

  After configuring the tag in XML, spring base-package can automatically scan the following sub-packets or following java file, if the scanning has @Component @Service @Controller like those annotated classes, these classes put registered as bean.

  Note: If you configure the <context: component-scan> then <context: annotation-config /> tag can no longer xml configuration because the former includes the latter. Further <context: annotation-config /> tag also provides two sub

  1. <context:include-filter>

  2. <context:exclude-filter>

  In the description of these two sub-label before, first talk about <context: component-scan> have a use-default-filters property, change the default attribute is true, which means all of the standard scans under specified package annotated class and registered as a bean so if only so written in the configuration file. <context: the Component-Scan base-Package Penalty for = "tv.huan.weisp.web" /> the Use-default-Filters is true that at this time will base All were under the java class -package packet or sub-packet scans, and the class is registered into the matching java bean.

  This scan can be found granularity bit too much, if you want to specify the following Controller scan package, how to do? At this time, the sub-label <context: incluce-filter> valor serves as a ground. 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>  

  This will only scan the specified base-package are under the java class @Controller, and registered into the bean. But because the use-dafault-filters and not specified in the above, it is by default true, so when the above configuration is changed as shown below and they will produce (note change in base-package package worth) and you expect inconsistent results

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

    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   

  </context:component-scan>  

  At this time, spring @Controller only scanned, the scanning further java classes specified subpackages of the package where the package annotations service @Service this time is not specified functions include-filter, as long as the use-default-filters arranged false on it. This can be avoided in the base-packeage configure multiple package names this is not a very elegant way to solve this problem.

Also in the project I was involved in base-package can be found in the package specified in sub-package does not contain any notes, so do not scan, then you can specify <context: exclude-filter> to filter, this package does not You need to be scanned. Based on the above description

  Case Use-dafault-filters = "false" is: <context: exclude-filter> is not specified scan, <context: include-filter> specified scan

Guess you like

Origin www.cnblogs.com/isme-zjh/p/11362774.html