从头认识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中的标签包括默认标签和自定义标签两种,而两种 ...
随机推荐
- js基础——运算符
爱创课堂前端培训--js基础 运算符一.运算符 运算符(Operators,也翻译为操作符),是发起运算的最简单形式.分类:(运算符的分类仁者见智,本课程进行一下分类.)数学运算符(Arithmeti ...
- day3--远程链接Linux
互联网上的计算机,都会有一个唯一的32位的地址,IP地址 我们访问服务器,就必须通过这个IP地址 局域网里也有预留的IP地址,192/10/172开头.局域网的IP地址也是唯一的. NAT模式,电脑宿 ...
- Python 解LeetCode:Intersection of Two Arrays
最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
- C#中窗体的close,dispose,以及application.exit()的区别
close关闭窗体,对象仍然存在,调用Close函数释放资源后可能还需要再次使用. dispose,销毁对象,窗体对象被回收了,也就是不存在了,Dispose函数释放的资源不再使用. applicat ...
- C# 程序集安装与卸载
下面我们来实现程序集的安装,代码如下: /// <summary>/// 程序集安装与卸载/// </summary>/// <param name="asse ...
- 在写一点关于MySQL的知识,感觉自己mmd
DBMS(Database Management System)数据库管理系统 包括有DDL(数据定义语言)和DML(数据操纵语言)以及DCL(数据库控制语言) 数据库设计方法: 1.需求分析阶段 ...
- [转载] Kafka+Storm+HDFS整合实践
转载自http://www.tuicool.com/articles/NzyqAn 在基于Hadoop平台的很多应用场景中,我们需要对数据进行离线和实时分析,离线分析可以很容易地借助于Hive来实现统 ...
- [转载] Quartz作业调度框架
转载自http://yangpanwww.iteye.com/blog/797563 Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提 ...
- .net core2.0下使用Identity改用dapper存储数据
前言. 已经好多天没写博客了,鉴于空闲无聊之时又兴起想写写博客,也当是给自己做个笔记.过了这么些天,我的文笔还是依然那么烂就请多多谅解了.今天主要是分享一下在使用.net core2.0下的实际遇到的 ...
- tungsten-replicator安装
需要环境 ruby1.8.5 or laterJava1.6 or later 应用程序对外开放接口 3306 (MySQL database)2112 (Tungsten THL)10000 (Tu ...