About component-scan operations (removal, invalidation)
The configuration items in this spring can scan the classes under our corresponding package, and automatically add the classes with @component, @service, @controller, @repository to the spring container for hosting
Note, component-scan will open annotation-config by default
But it's not just that, we can limit the scope of it
We can include and exclude through include-fileter and exclude-filter
Note that there are 5 ways to select our range!
Before we exclude:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> < context:component-scan base-package = "cn.cutter" > <!-- Control the scan range, enter the container according to the annotation scan --> < context:include-filter type = "annotation" expression = "cn.cutter" /> <!-- <context:exclude-filter type="aspectj" expression="* cn.cutter.noscan.*"/> --> </ context:component-scan > <!-- <context:annotation -config /> This property is already true by default in the component --> <!-- 国际化配置 --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>i18/users</value> <value>i18/errormsg</value> </list> </property> </bean> <bean id="ttmRateService" class="cn.cutter.simplefx.service.impl.MockTTMRateServiceImpl"></bean> <bean id="fooBar" class="cn.cutter.start.resourceloader.FooBar" /> <bean id="fooBar2" class="cn.cutter.start.resourceloader.FooBar2" /> <bean id="resourceDemo" class="cn.cutter.start.resourceloader.ResourceDemo"> <property name="resource"> <value>classpath:applicationContext-bean.xml</value> </property> </bean> <!-- <bean id="myObject" class="cn.cutter.start.bean.MyObject"></bean> --> <!-- <alias name="FXNewsProvider" alias="provideralias"/> --> <!-- <bean id="test4key2" ></bean> <bean id="" class="..." lazy-init="true"> <constructor-arg> <ref bean="" parent=""/> </constructor-arg> <property name="test1"> <value>ttttt1</value> <idref bean="ttttt1"/> </property> <property name="test2"> <list> <value>test2value</value> <ref bean="test2222"/> <idref bean="test22222"/> <bean class="..."></bean> </list> </property> <property name="test3"> <set> <value>test2value</value> <ref bean="test2222"/> <idref bean="test22222"/> <bean class="..."></bean> </set> </property> <property name="test4"> <map> <entry key="test4key1"> <value>something</value> </entry> <entry key-ref="test5key2"> <list> <value>test2value</value> <ref bean="test2222"/> <idref bean="test22222"/> <bean class="..."></bean> </list> </entry> </map> </property> </bean> --> </beans>
test
@Test public void testScan() { ApplicationContext ctx = this.before(); //TestScan TestScan testScan = (TestScan) ctx.getBean("testScan"); if(testScan == null) { System.out.println( "Scan Exclude" ); } else { System.out.println( "Scan in" ); testScan.sayHello(); } }
Results show: