@ComponentScan的使用

89 篇文章 2 订阅
45 篇文章 1 订阅
 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>

在早期的时候使用的是bean.xml的context:component-scan去扫描的包

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       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.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!--扫描包-->
    <context:component-scan base-package="com.dmg">

    </context:component-scan>


</beans>
public class Test {

    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
        String[] beanDefibnitionNames = applicationContext.getBeanDefinitionNames();
        for (String s : beanDefibnitionNames) {
            System.out.println(s);
        }

    }
}

但是我们使用@ComponentScan(value = "com.dmg")

这个注解,去扫描某一个包,就不用在xml去配置了

@Component
public class TestComponent {
}
@Controller
public class TestController {
}
@Service
public class TestService {
}

@ComponentScan(value = "com.dmg")
public class TestConfig {

}
public class Test {

    public static void main(String[] args) {
        ApplicationContext applicationContext=new AnnotationConfigApplicationContext(TestConfig.class);
        String[] beanDefibnitionNames = applicationContext.getBeanDefinitionNames();
        for (String s : beanDefibnitionNames) {
            System.out.println(s);
        }

    }
}

可以看到,@Component,@Controller,@Service这些注解都扫描进来了

只要加了这些注解,被扫描到,那么就会加载到ioc容器中

我们也可以做使用excludeFilters来进行过滤,比如排除@Controller,和@Service的注解

@ComponentScan(value = "com.dmg",excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
})
public class TestConfig {

}

我们还可以使用includeFilters只使用某一些注解

结合useDefaultFilters = false使用默认筛选器,要不然不生效

@ComponentScan(value = "com.dmg",includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class, Service.class})
},useDefaultFilters = false)
public class TestConfig {

}

FilterType.ANNOTATION是使用注解的类型

我们还可以使用 FilterType.ASSIGNABLE_TYPE,来指定一个类

public class Sp {
}
@ComponentScan(value = "com.dmg",includeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = {Sp.class})
},useDefaultFilters = false)
public class TestConfig {

}

我们还可以设置自定义的类型FilterType.CUSTOM

创建一个MyFilter类实现TypeFilter


@ComponentScan(value = "com.dmg",includeFilters = {
            @ComponentScan.Filter(type = FilterType.CUSTOM,classes = {MyFilter.class})
},useDefaultFilters = false)
public class TestConfig {

}
public class MyFilter implements TypeFilter {


    /**
     *
     * @param metadataReader  读取当前正在扫描的类信息
     * @param metadataReaderFactory 可以获取到其他类的任何信息
     * @return
     * @throws IOException
     */
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        //获取当前类注解信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        //获取当前正在扫描的类信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        //获取资源信息
        Resource resource = metadataReader.getResource();
        //获取类路径信息
        String className = classMetadata.getClassName();
        System.out.println("============"+className);
        //模拟是Sp这个类生效 注入到ioc容器中
        if(className.contains("Sp")){
            return true;
        }
        //不注入到ioc容器中
        return false;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值