这一章节我们来讨论一下过滤器<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
时间: 2024-10-25 02:59:57