码迷,mamicode.com
首页 > 编程语言 > 详细

Spring context:component-scan中使用context:include-filter和context:exclude-filter

时间:2015-09-28 14:45:07      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/stono/p/4843864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!