标签:
这一章节我们来讨论一下过滤器<context:include-filter/>的使用。
1.domain
Person接口:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_20; public interface Person { }
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_20; 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_20; 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; } }
2.测试类:
package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_20; 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_20/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()); } }
<?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_20"> <context:include-filter type="assignable" expression="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_20.Person" /> </context:component-scan> </beans>
(1)我们配置自动扫描<context:component-scan/>
(2)在自动扫描里面我们在配置<context:include-filter/>,然后type=“assignable”,这里的意思是,Person接口所派生出来的所有类,都自动注册bean
type的四种参数:
assignable-指定扫描某个接口派生出来的类
annotation-指定扫描使用某个注解的类
aspectj-指定扫描AspectJ表达式相匹配的类
custom-指定扫描自定义的实现了org.springframework.core.type.filter.TypeFilter接口的类
regex-指定扫描符合正则表达式的类
4.使用场景
上面我们只是演示了第一种,因为有些时候我们是没有源码或者一个接口派生n多实现类的情况,这种场景使用这个最为合适。
测试输出:
jack
mike
总结:这一章节我们主要介绍了过滤器<context:include-filter/>的使用方法与使用场景。
目录:http://blog.csdn.net/raylee2007/article/details/50611627
我的github:https://github.com/raylee2015/my_new_spring
从头认识Spring-2.7 自动检测Bean(2)-过滤器<context:include-filter/>
标签:
原文地址:http://blog.csdn.net/raylee2007/article/details/50685862