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的更多相关文章

  1. 关于context:component-scan配置中use-default-filters参数的作用

    参考了多篇文章都说明了use-default-filters参数的基本用途,但有些主要点没有说到,这里补充记录下: <context:component-scan base-package=&q ...

  2. [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 ...

  3. spring学习笔记三:Component注解(把POJO类实例化到spring的IOC容器中)

    Component注解:把普通的POJO 类实例化到spring的IOC容器中,就是定义成<bean id="" class=""> 项目目录树: ...

  4. Android Application中的Context和Activity中的Context的异同

    一.Context是什么: 1.Context是维持Android程序中各组件能够正常工作的一个核心功能类,我们选中Context类 ,按下快捷键F4,右边就会出现一个Context类的继承结构图啦, ...

  5. Android中的this、Activity、Context等

    Android中的this.Activity.Context.Application等虽然有相似之处,但是不能乱用,每一个都有自己的特点.用的时候不能太随意了. 避免context相关的内存泄露,注意 ...

  6. [知识库:python-tornado]异步调用中的上下文控制Tornado stack context

    异步调用中的上下文控制Tornado stack context https://www.zouyesheng.com/context-in-async-env.html 这篇文章真心不错, 非常透彻 ...

  7. Spring注解Component原理源码解析

    在实际开发中,我们经常使用Spring的@Component.@Service.@Repository以及 @Controller等注解来实现bean托管给Spring容器管理.Spring是怎么样实 ...

  8. 关于 spring MVC 配置自动扫描中 use-default-filters 属性

    1.springMVC 设置扫描 Bean 的两种常见写法 1.1.先看第一种常见的配置:默认 <!-- 配置Controller扫描 --> <context:component- ...

  9. Spring 注解@Component,@Service,@Controller,@Repository

    Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...

  10. 创建swagger的springboot-stater,并在spring cloud zuul网关中引入

    Swagger 是一款RESTFUL接口的.基于YAML.JSON语言的文档在线自动生成.代码自动生成的工具. 通过在controller中添加注解,即可轻易实现代码文档化. Swagger提供ui界 ...

随机推荐

  1. PHP实现畅言留言板和网易跟帖样式

    原文:http://justcoding.iteye.com/blog/2251192   我要实现的就是下图的这种样式,可参考下面这两个网站的留言板,他们的实现原理都是一样的 http://chan ...

  2. Django之路: 基本命令与网址进阶

    一.Django 基本命令 温馨提示:如果你想学习Django,那么就请您从现在开始按照笔记记录一步一步的用手把代码敲出来,千万不要偷懒哦..... 1.创建一个Django project djan ...

  3. POP音原因

    一,通话时调节音量,有POP音. POP音产生原因在于,音量变化太大导致有POP音,需要以淡入淡出的方式调节音量.请申请MOLY00108114 & MOLY00108143这两个Modem ...

  4. 转 互联网推送服务原理:长连接+心跳机制(MQTT协议)

    http://blog.csdn.net/zhangzeyuaaa/article/details/39028369 目录(?)[-] 无线移动网络的特点 android系统的推送和IOS的推送有什么 ...

  5. PHP 领域模型与数据库映射文章

    1. http://blog.csdn.net/happen_zhang/article/details/12761747 2. http://blog.csdn.net/hguisu/article ...

  6. 如何理解java的引用传递

    1. 数组的引用传递 public class TestArray { public static void changeAry1(int[] ary){ int[] ary1 = {9,9,9}; ...

  7. Frequent Distribution sorted by frequency

    import nltk def freq_sorted(text,ranklimit): fd=nltk.FreqDist(text) cumulative = 0.0 for rank, (word ...

  8. 改变导航栏title字体的大小和颜色

    方法一:自定义视图的方法 就是在导航向上添加一个titleView,可以使用一个label,再设置label的背景颜色透明,字体什么的设置就很简单了. //自定义标题视图 UILabel *title ...

  9. Android SQLite总结[转载]

    [转载] :http://blog.163.com/zqy216_2008/blog/static/4119371820119954812509/ 最近在做的项目涉及到了SQLite,大学时没有好好学 ...

  10. STM32-USB详细使用说明(转)

    源:STM32-USB详细使用说明 附件HID的双向通信 亮点STM32开发板充实了USBHID数据发送和接收例程(STM32固件库3.5 USB库3.4)