从头认识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
从头认识Spring-2.7 自己主动检測Bean(2)-过滤器<context:include-filter/>的更多相关文章
- 从头认识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; ...
- 自己主动检測&后台复制光盘内容
原理:利用python的win32模块,注冊服务,让代码在后台执行,检測光盘并复制文件 启动的方法就是直接在cmd下,main.py install ,然后去windows 的服务下就能够看到The ...
- Android Studio代码自己主动检測错误提示
Android Studio的代码自己主动检測的错误提示方式感觉有点奇葩.和Eclipse区别非常大,Eclipse检測到某个资源文件找不到或者错误,都会在Project中相应的文件前面打叉.可是An ...
- 使用UIDataDetectorTypes自己主动检測电话、网址和邮箱
支付宝公布最新版本号9.0.再一次引发一场撕逼大战.微信说支付宝抄袭了它.支付宝说微信一直都在抄袭自己.在我看来.微信和支付宝都抄袭了对方.对于大佬们的抄袭.我们也是司空见惯了. 支付宝这一次更新,真 ...
- Android自己主动检測版本号及自己主动升级
步骤: 1.检測当前版本号的信息AndroidManifest.xml-->manifest-->android:versionName. 2.从server获取版本号号(版本号号存在于x ...
- Spring源码学习(5)—— bean的加载 part 2
之前归纳了从spring容器的缓存中直接获取bean的情况,接下来就需要从头开始bean的加载过程了.这里着重看单例的bean的加载 if(ex1.isSingleton()) { sharedIns ...
- Spring IOC(三)单例 bean 的注册管理
Spring IOC(三)单例 bean 的注册管理 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 在 Spring 中 ...
- Spring Cloud (十三) Zuul:静态路由、静态过滤器与动态路由的实现
前言 本文起笔于2018-06-26周二,接了一个这周要完成的开发任务,需要先等其他人的接口,可能更新的会慢一些,还望大家见谅.这篇博客我们主要讲Spring Cloud Zuul.项目地址:我的gi ...
- Spring源码分析(七)bean标签的解析及注册
摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 在上一篇中提到过Spring中的标签包括默认标签和自定义标签两种,而两种 ...
随机推荐
- linux-echo
echo 更新时间: 2017-10-11-11:55:24 echo:打印输出内容 参数选择 -e 激活转义字符 命令:echo 123 ,此命令 就会输出123 命令: echo -e &q ...
- wordpress搬家到 linode 步骤简析
1. 购买并安装系统 购买就不说了哈,英文不好的自己搜教程. 然后是安装系统 linode 系统安装: 购买完成后登录,进入找到购买的vps ,点击 Dashboard (控制面板) 进入后点击 面板 ...
- SpringMVC接收复杂对象
SpringMVC接收复杂对象 转载请注明地址:http://www.cnblogs.com/funnyzpc/p/7642977.html 本节内容暂放一边,我先扯点儿,心情好了,代码顺风顺水哈~ ...
- JAVA调用matlab代码
做实验一直用的matlab代码,需要嵌入到java项目中,matlab代码拼拼凑凑不是很了解,投机取巧采用java调用matlab的方式解决. 1. matlab版本:matlabR2014a ...
- [转载] OAuth2.0认证和授权原理
转载自http://www.tuicool.com/articles/qqeuE3 什么是OAuth授权? 一.什么是OAuth协议 OAuth(开放授权)是一个开放标准,允许第三方网站在用户授权的前 ...
- [转]ORACLE分区表的使用和管理
转自:http://love-flying-snow.iteye.com/blog/573303 废话少说,直接讲分区语法. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一: ...
- [欧拉路径]Play on Words UVA10129
传送门: UVA - 10129 题目大意: 给定一些单词(可能会重复出现),判断单词是否能排成一个序列,前提是单词的最后一个字母与下一个单词的第一个字母相同.输出"The door c ...
- linux如何在日志中查找关键字、前几行、结尾几行
如何使用命令行快速查看项目日志是每个开发人员必备技能,尤其在没有专门日志搜集系统的情况下,想要知道目前项目运行状态最好的办法就是打开log日志一瞅即明白. 复杂的到用时再查不晚,但是简单的还是有必要掌 ...
- 5分钟搞定iOS抓包Charles,让数据一清二楚
Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装 官网下载安装Charles:https://www.charlesproxy.com/download/ 2. HTTP ...
- Maven多模块的开发项目搭建
系统越复杂,所有的业务逻辑都放在一个项目里,各个包之间的业务逻辑相互调用,这样添加了开发成本,同时对之后的系统维护,错误排查带来一定的麻烦. 通过Maven的多模块开发,把一个系统拆分成多个模块,通过 ...