从头认识Spring-2.7 自己主动检測Bean(3)-过滤器<context:exclude-filter/>
这一章节我们来讨论一下过滤器<context:exclude-filter/>的使用。
1.domain
Person接口:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_21; public interface Person { }
拳击手类:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_21; import org.springframework.beans.factory.annotation.Value; public class Fighter implements Person { @Value("mike") private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } }
厨师类:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_21; import org.springframework.beans.factory.annotation.Value; public class Chief implements Person { @Value("jack") private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } }
在上面的类里面我们有益没有使用不论什么标记bean的注解,并且我们再配置文件中面也不会显示标记bean
測试实体类:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_21; import org.springframework.stereotype.Component; @Component public class MyComponent implements Person { }在这个类里面我们标记了@Component注解。一会儿我们会使用过滤器排除这个注解的全部bean
2.測试类:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_21; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_21/ApplicationContext-test.xml" }) public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief() { Chief jack = applicationContext.getBean(Chief.class); System.out.println(jack.getName()); Fighter mike = applicationContext.getBean(Fighter.class); System.out.println(mike.getName()); System.out.println(applicationContext.containsBean("myComponent")); } }
測试的最后面我们仅仅是输出是否那个类已经生成对象
3.配置文件(重点)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_21"> <context:include-filter type="assignable" expression="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_21.Person" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component" /> </context:component-scan> </beans>
在配置文件中面:
(1)我们配置自己主动扫描<context:component-scan/>
(2)在自己主动扫描里面我们在配置<context:include-filter/>。然后type=“assignable”,这里的意思是。Person接口所派生出来的全部类,都自己主动注冊bean
(3)还配置了<context:exclude-filter/>。全部被注解@Component标记的类我们都不自己主动注冊
測试输出:
jack
mike
false
总结:这一章节我们主要介绍了过滤器<context:exclude-filter/>的用法。
文件夹:http://blog.csdn.net/raylee2007/article/details/50611627
我的github:https://github.com/raylee2015/my_new_spring