从头认识Spring-2.7 自动检测Bean(2)-过滤器<context:include-filter/>

这一章节我们来讨论一下过滤器<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;
	}

}

在上面的类里面我们故意没有使用任何标记bean的注解,而且我们再配置文件里面也不会显示标记bean


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());
	}
}

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_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


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值