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 + "]";
    }
}
时间: 2024-08-10 16:57:33

Spring context:component-scan中使用context:include-filter和context:exclude-filter的相关文章

在完全由Spring管理的环境中使用Spring的Context获取Bean实例

在大型的应用中,常常会有很灵活的需求,而在使用了框架之后,虽然可以大大提高开发的效率,但同时,也把我们框到一个架子中了. 下面先说一下我遇到的问题,事情大概是这样的: @Component @Scope("prototype") public class Action1 implements Action{ ..... } @Component @Scope("prototype") public class Action2 implements Action{ .

关于spring”通配符的匹配很全面, 但无法找到元素 &#39;context:component-scan&#39; 的声明“的错误

关于spring配置的问题 近日学习spring时遇到了这个问题: [html] view plain copy org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from URL [file:/F: /workspace/spring/target/classes /applicationContext.xml] is invalid; nested ex

spring项目后出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoade

导入别人的spring项目后出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoade错误, 解决: 1.若项目的主人是用maven创建spring项目, 解决办法: 项目 -> 属性 -> Deployment Assembly -> Add -> Java Build Path Entries -> 选择Maven Dependencies -> Finish -&

“上下文关键字”KWIC(Key Word in Context,文本中的关键字)检索系统尝试

一.实验目的 1.理解不同体系结构风格的具体内涵. 2.学习体系结构风格的具体实践. 二.实验环境 硬件: (依据具体情况填写) 软件:Java或任何一种自己熟悉的语言 三.实验内容 "上下文关键字"KWIC(Key Word in Context,文本中的关键字)检索系统接受有序的行集合:每一行是单词的有序集合:每一个单词又是字母的有序集合.通过重复地删除航中第一个单词,并把它插入行尾,每一行可以被"循环地移动".KWIC检索系统以字母表的顺序输出一个所有行循环移

Spring注解Component原理源码解析

在实际开发中,我们经常使用Spring的@Component.@Service.@Repository以及 @Controller等注解来实现bean托管给Spring容器管理.Spring是怎么样实现的呢?我们一起跟着源码看看整个过程吧! 照旧,先看调用时序图: public AnnotationConfigApplicationContext(String... basePackages) { this(); scan(basePackages); refresh(); } Spring启动

Spring注解@Component、@Repository、@Service、@Controller区别

Spring注解@Component.@Repository.@Service.@Controller区别 Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3 个注释和 @Comp

Spring中用@Component、@Repository、@Service和 @Controller等标注的默认Bean名称会是小写开头的非限定类名

今天用调度平台去调用bean中的方法时,提示找不到bean.经查,发现是由于如果在标注上没有提供name属性值,则默认的bean名称是小写开头的,而不是大写开头的. 下面是其他文档参阅: 使用过滤器自定义扫描 <context:component-scan base-package="org.example"> spring开发手册中这样写: 3.12.4. 自动检测组件的命名 当一个组件在某个扫描过程中被自动检测到时,会根据那个扫描器的BeanNameGenerator 

Spring注解@Component、@Repository、@Service、@Controller的相关知识

Spring注解@Component.@Repository.@Service.@Controller区别 spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3 个注释和 @Comp

(转载)Spring 注解@Component,@Service,@Controller,@Repository

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层.业务层和控制层(Web 层)相对应.虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能.所以,如果 Web 应

spring在IoC容器中装配Bean详解

1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean实例放入bean缓存池: 应用程序使用bean. 1.2.基于xml的配置 (1)xml文件概述 xmlns------默认命名空间 xmlns:xsi-------标准命名空间,用于指定自定义命名空间的schema文件 xmlns:xxx="aaaaa"-------自定义命名空间,xx