Spring:@ComponentScan 使用

 

编写不易,转载请注明(http://shihlei.iteye.com/blog/2405675)!

 

一 @ComponentScan 概述

扫描指定表下的Component(@Componment,@Configuration,@Service 等等)

 

存在于:org.springframework.context.annotation 包中

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version${spring.version}</version>
</dependency>

 

二 使用Demo


 

(1)数据准备

package:x.demo.spring.context.scan.pack1

package x.demo.spring.context.scan.pack1;

import org.springframework.stereotype.Component;

@Component
public class Component1 {
    
}

package x.demo.spring.context.scan.pack1;

import org.springframework.stereotype.Component;

@Component
public class Component2 {

}

 

package:x.demo.spring.context.scan.pack3

 

package x.demo.spring.context.scan.pack3;

import org.springframework.stereotype.Component;

@Component
public class Component3 {

}

 

(2)触发Demo

方法1 :代码方式集成:通过AnnotationConfigApplicationContext 触发@ComponentScan使用

 

package x.demo.spring.context.scan;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import x.demo.spring.context.scan.pack1.Component2;
import x.demo.spring.context.scan.pack3.Component3;

@ComponentScan(basePackages = {"x.demo.spring.context.scan.pack1"},
        basePackageClasses = {Component3.class},
        excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Component2.class))
public class ComponentScanAnnotationDemo {

    //打印相关信息
    private static void show(ApplicationContext context) {
        String[] beans = {"component1", "component2", "component3"};

        for (String b : beans) {
            boolean isContains = context.containsBean(b);
            System.out.println(b + " is exist : " + isContains);
            if (isContains) {
                System.out.println(b + " : " + context.getBean(b));
            }
        }
    }

    /**
     * 通过AnnotationConfigApplicationContext 触发@ComponentScan使用
     */
    public static void triggerViaAnnotationConfigApplicationContext() {
        //加载@ComponentScan 的包扫描
        try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();) {
            context.register(ComponentScanAnnotationDemo.class);
            context.refresh();

            show(context);
        }
    }

    public static void main(String[] args) {
        ComponentScanAnnotationDemo.triggerViaAnnotationConfigApplicationContext();
    }
}

 

方法2:xml 方式触发@ComponentScan使用

(a)xml路径:classpath:x.demo.spring.context.scan/spring-scan.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <context:annotation-config/>
    <bean class="x.demo.spring.context.scan.ComponentScanAnnotationDemo"/>
</beans>

 (b)程序:

 

package x.demo.spring.context.scan;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import x.demo.spring.context.scan.pack1.Component2;
import x.demo.spring.context.scan.pack3.Component3;

@ComponentScan(basePackages = {"x.demo.spring.context.scan.pack1"},
        basePackageClasses = {Component3.class},
        excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Component2.class))
public class ComponentScanAnnotationDemo {

    //打印相关信息
    private static void show(ApplicationContext context) {
        String[] beans = {"component1", "component2", "component3"};

        for (String b : beans) {
            boolean isContains = context.containsBean(b);
            System.out.println(b + " is exist : " + isContains);
            if (isContains) {
                System.out.println(b + " : " + context.getBean(b));
            }
        }
    }

    /**
     * xml 方式触发@ComponentScan使用
     */
    public static void triggerViaXml() {

        try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext
                ("classpath:x.demo.spring.context.scan/spring-scan.xml");) {
            show(context);
        }
    }

    public static void main(String[] args) {
        ComponentScanAnnotationDemo.triggerViaXml();
    }
}

 

(3)结果

component1 is exist : true
component1 : x.demo.spring.context.scan.pack1.Component1@17baae6e
component2 is exist : false
component3 is exist : true
component3 : x.demo.spring.context.scan.pack3.Component3@69379752

 

三 常用注解属性设置

     (1)basePackages:指定扫描的包,basePackageClasses:指定扫描的类

 

     (2)useDefaultFilters:取消默认filter设置

      默认Filter自动扫描包下的Component,取消相当于禁用某个扫描

 

     (3)@ComponentScan.Filter:

      指定Filter 规则,可用于excludeFilters 排除Filters ,includeFilters 包含Filters(包含的Filters 在禁用默认Filter使用,否则没有意义) 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值