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

从头认识Spring-2.7 自己主动检測Bean(2)-过滤器&lt;context:include-filter/&gt;的更多相关文章

  1. 从头认识Spring-2.7 自己主动检測Bean(1)-@Component @Repository @Service @Controller

    这一章节我们来讨论一下自己主动检測Bean. 1.domain 厨师类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19; ...

  2. 自己主动检測&后台复制光盘内容

    原理:利用python的win32模块,注冊服务,让代码在后台执行,检測光盘并复制文件 启动的方法就是直接在cmd下,main.py install ,然后去windows 的服务下就能够看到The ...

  3. Android Studio代码自己主动检測错误提示

    Android Studio的代码自己主动检測的错误提示方式感觉有点奇葩.和Eclipse区别非常大,Eclipse检測到某个资源文件找不到或者错误,都会在Project中相应的文件前面打叉.可是An ...

  4. 使用UIDataDetectorTypes自己主动检測电话、网址和邮箱

    支付宝公布最新版本号9.0.再一次引发一场撕逼大战.微信说支付宝抄袭了它.支付宝说微信一直都在抄袭自己.在我看来.微信和支付宝都抄袭了对方.对于大佬们的抄袭.我们也是司空见惯了. 支付宝这一次更新,真 ...

  5. Android自己主动检測版本号及自己主动升级

    步骤: 1.检測当前版本号的信息AndroidManifest.xml-->manifest-->android:versionName. 2.从server获取版本号号(版本号号存在于x ...

  6. Spring源码学习(5)—— bean的加载 part 2

    之前归纳了从spring容器的缓存中直接获取bean的情况,接下来就需要从头开始bean的加载过程了.这里着重看单例的bean的加载 if(ex1.isSingleton()) { sharedIns ...

  7. Spring IOC(三)单例 bean 的注册管理

    Spring IOC(三)单例 bean 的注册管理 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 在 Spring 中 ...

  8. Spring Cloud (十三) Zuul:静态路由、静态过滤器与动态路由的实现

    前言 本文起笔于2018-06-26周二,接了一个这周要完成的开发任务,需要先等其他人的接口,可能更新的会慢一些,还望大家见谅.这篇博客我们主要讲Spring Cloud Zuul.项目地址:我的gi ...

  9. Spring源码分析(七)bean标签的解析及注册

    摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 在上一篇中提到过Spring中的标签包括默认标签和自定义标签两种,而两种 ...

随机推荐

  1. CentOS、Ubuntu配置网卡子接口

    CentOS # ip addr add dev eth0 lable eth0: 以上为临时配置,重启失效.若需永久保存,增加网络配置文件 # vim /etc/sysconfig/network- ...

  2. ④bootstrap列表使用基础案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. RestSharp使用总结

    RestSharp是一个轻量的,不依赖任何第三方的组件或者类库的Http的组件.RestSharp具有以下的优点:   1.支持.NET 3.5+,Silverlight 4, Windows Pho ...

  4. jQuery学习笔记之Ajax用法详解

    这篇文章主要介绍了jQuery学习笔记之Ajax用法,结合实例形式较为详细的分析总结了jQuery中ajax的相关使用技巧,包括ajax请求.载入.处理.传递等,需要的朋友可以参考下 本文实例讲述了j ...

  5. 教你如何实现微信小程序与.net core应用服务端的无状态身份验证

    随着.net core2的发布,越来越多人使用.net core2开发各种应用服务端,下面我就结合自己最近开发的一款小程序,给大家分享下,怎么使用小程序登录后,小程序与服务端交互的权限控制. .net ...

  6. [转载] PHP工作模型与运行机制

    转载自http://www.nowamagic.net/librarys/veda/detail/350 PHP的工作模型非常特殊.从某种程度上说,PHP和ASP.ASP.NET.JSP/Servle ...

  7. 创建内部的yum源

    http://www.phy.duke.edu/~rgb/General/yum_HOWTO/yum_HOWTO/yum_HOWTO.html#toc6.2 主要的步骤包括: 1.创建Reposito ...

  8. linux平台搭建postfix邮件服务器

    一,搭建邮件服务器前准备如下: Centos 7.2 64位Postfix-2.8.12.tar.gz Postfix MTA(邮件传输代理)Dovecot-2.1.8.tar.gz IMAP 和 P ...

  9. 结合提供者模式解析Jenkins源码国际化的实现

    关键字:提供者模式,设计模式,github,gerrit,源码学习,jenkins,国际化,maven高级,maven插件 本篇文章的源码展示部分由于长度问题不会全部粘贴展示,或许只是直接提及,需要了 ...

  10. 安装freemarker模板的ftl插件

    安装freemarker模板的ftl插件 同意协议 等待运行完成 重新启动eclipse 查看是否生效