Spring context:component-scan中使用context:include-filter和context:exclude-filter
Spring context:component-scan中使用context:include-filter和context:exclude-filter
XML:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="com.stono.sprtest">
<context:include-filter type="assignable" expression="com.stono.sprtest.InstrumentI" />
<context:exclude-filter type="annotation" expression="com.stono.sprtest.SkipIt"/>
</context:component-scan>
</beans>
AppBean:
package com.stono.sprtest; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppBeans7 {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans7.xml");
Singer2 singer2 = (Singer2) context.getBean("singer");
System.out.println(singer2);
// Object bean = context.getBean("cymbal"); // 已经把Cymbal排除了;
// System.out.println(bean);
}
}
Annotation:
package com.stono.sprtest;
public @interface SkipIt {
}
POJO:
package com.stono.sprtest; @SkipIt
public class Cymbal implements InstrumentI {
}
package com.stono.sprtest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("singer")
public class Singer2 {
@Autowired
// @Component标注的类,其Id是camel-casing类名;两个Intrument接口实现类,必须指定其中的一个;
@Qualifier("saxophone")
private InstrumentI instrument;
@Value("justValue2")
private String name;
@Override
public String toString() {
return "Singer2 [instrument=" + instrument + ", name=" + name + "]";
}
}
Spring context:component-scan中使用context:include-filter和context:exclude-filter的更多相关文章
- 关于context:component-scan配置中use-default-filters参数的作用
参考了多篇文章都说明了use-default-filters参数的基本用途,但有些主要点没有说到,这里补充记录下: <context:component-scan base-package=&q ...
- [Spring Boot] Use Component Scan to scan for Bean
Component Scan is important concept when we want to create Bean. Currently we know what, for the cla ...
- spring学习笔记三:Component注解(把POJO类实例化到spring的IOC容器中)
Component注解:把普通的POJO 类实例化到spring的IOC容器中,就是定义成<bean id="" class=""> 项目目录树: ...
- Android Application中的Context和Activity中的Context的异同
一.Context是什么: 1.Context是维持Android程序中各组件能够正常工作的一个核心功能类,我们选中Context类 ,按下快捷键F4,右边就会出现一个Context类的继承结构图啦, ...
- Android中的this、Activity、Context等
Android中的this.Activity.Context.Application等虽然有相似之处,但是不能乱用,每一个都有自己的特点.用的时候不能太随意了. 避免context相关的内存泄露,注意 ...
- [知识库:python-tornado]异步调用中的上下文控制Tornado stack context
异步调用中的上下文控制Tornado stack context https://www.zouyesheng.com/context-in-async-env.html 这篇文章真心不错, 非常透彻 ...
- Spring注解Component原理源码解析
在实际开发中,我们经常使用Spring的@Component.@Service.@Repository以及 @Controller等注解来实现bean托管给Spring容器管理.Spring是怎么样实 ...
- 关于 spring MVC 配置自动扫描中 use-default-filters 属性
1.springMVC 设置扫描 Bean 的两种常见写法 1.1.先看第一种常见的配置:默认 <!-- 配置Controller扫描 --> <context:component- ...
- Spring 注解@Component,@Service,@Controller,@Repository
Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...
- 创建swagger的springboot-stater,并在spring cloud zuul网关中引入
Swagger 是一款RESTFUL接口的.基于YAML.JSON语言的文档在线自动生成.代码自动生成的工具. 通过在controller中添加注解,即可轻易实现代码文档化. Swagger提供ui界 ...
随机推荐
- c# 添加了按钮双击事件后,再删除掉代码会提示错误
有两种方法:.清空属性窗口中的双击事件(doubleclick )右边的内容: .单击“发生错误”提示窗口的“否”后,再双击错误列表里的错误项,此时编辑窗口跳转为xx.Designer.cs,然后注释 ...
- JSDOM优化
JS操作DOM,就像从一个岛到另外一个岛,过桥的时候,每次都要收取过桥费,尽量减少过桥的次数.列如: for循环插入到页面5000个LI: 应该 先用一个变量存 5000个li,然后一次插入. ...
- 前端UI
一个非常好的前端UI,值得研究下 http://semantic-ui.com/
- 请教<context:component-scan/>和<mvc:annotation-driven/>的区别20
http://www.iteye.com/problems/66133 FileSystemXmlApplicationContext
- Android Guts: Intro to Loopers and Handlers
One of the reasons I love Android API is because it contains so many useful little things. Many of t ...
- CSS的继承性和层叠性
1.继承性 比不是所有的属性都能继承,只有一些文本的属性才能被继承.比如:color,text-开头的,line-开头的,font-开头的.这几种属性才能被继承. 其余的关于盒子,定位,布局的都不能被 ...
- IIS7无后缀URL部署问题 MVC4 MVC URL映射 windows server 2008
前言和中间一段都是我找到问题的过程和思维方法.没兴趣的可以直接跳过看后面的问题和解决. 前言: 问题发生在站点完成后,部署到服务器上.以为这个是最轻松的工作.结果悲剧了.windows server ...
- Unity3d之协程自实现测试
using UnityEngine; using System.Collections; public class TestStartCoroutine : MonoBehaviour { IEnum ...
- shell之crontab
一 crontab高级用法 Crontab 主要是在做排程, 通常一般寫法大概都是如下: 0 0 * * * /usr/local/www/awstats/cgi-bin/awstats.sh 這種 ...
- AWK第一篇------全面介绍
AWK-文本流编辑器 目录 [隐藏] 1 命令行语法 2 用shell实现调用awk 3 awk语言概要 3.1 记录和字段 3.2 脚本的格式 3.3 行为终止 3.4 注释 3.5 模式 3.6 ...