一、@ComponentScan的作用

  • @ComponentScan用于类或接口上,主要是指定扫描路径并把带有指定注解的类注册到Spring容器中。
  • 会被自动装配的注解包括@Component、@Bean、@Controller、@Service、@Repository等等。

二、定义组件

1. @Service注解的类

MyService类被扫描后,会生成名为myBeanService的实例

package info.pigg.study.java.service;

import org.springframework.stereotype.Service;

@Service("myBeanService")
public class MyService {
}

2. @Configuration+@Bean

MyConfig 类被扫描后,会生成名为myBeanPerson和myBeanUser两个实例

package info.pigg.study.java.config;

@Configuration
public class MyConfig {

    @Bean(name = "myBeanPerson")
    public Person myBeanPerson(){
        return new Person("king", 31);
    }

    @Bean(name = "myBeanUser")
    public User myBeanUser(){
        return new User("king", 31);
    }
}

三、在主程序类中测试

上面定义的组件都属于"info.pigg.study.java",所以添加@ComponentScan(value = “info.pigg.study.java”)

@SpringBootApplication
@ComponentScan(value = "info.pigg.study.java")
public class DictApplication {

    public static void main(String[] args) {
    
        ConfigurableApplicationContext run = SpringApplication.run(DictApplication.class, args);
        String[] names = run.getBeanDefinitionNames();
		
		//打印出名称包含myBean的实例
        for (String name : names) {
            if (name.contains("myBean")) {
                System.out.println(name);
            }
        }
    }
}

测试结果如下:

myBeanService
myBeanPerson
myBeanUser

四、@ComponentScan中excludeFilters使用

在@ComponentScan可以设置includeFilters和excludeFilters,来自定义过滤器。一般excludeFilters用的比较多。

1. 过滤指定的类名

type = FilterType.ASSIGNABLE_TYPE是根据类class来过滤,后面classes指向类名

@SpringBootApplication
@ComponentScan(value = "info.pigg.study.java", excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {MyService.class})})
public class DictApplication {
	//和上面一样,省略
}

测试结果如下:

myBeanPerson
myBeanUser

2. 过滤指定的注解

在"info.pigg.study.java"包和子包下,排除有@Service注解的类

@SpringBootApplication
@ComponentScan(value = "info.pigg.study.java", excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Service.class})})
public class DictApplication {
}

测试结果如下:

myBeanPerson
myBea

3. 自定义过滤

type = FilterType.CUSTOM,是自定义过滤,classes 指定的类要实现TypeFilter接口,在match方法中可以获取当前扫描到的类的信息,比如注解、类名和类路径。

@SpringBootApplication
@ComponentScan(value = "info.pigg.study.java", excludeFilters = {
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})})
public class DictApplication {
}

下面举例:在类名包含"MyService"时,match方法返回true,这样在excludeFilters时,包含"MyService"的类就会被排除掉。

package info.pigg.study.java.filter;

import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

public class MyTypeFilter implements TypeFilter {

    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {

        //获取当前类注解的信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        
        //获取当前类资源(类的路径)
        Resource resource = metadataReader.getResource();

        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        System.out.println("当前正在被扫描的类的类名" + classMetadata.getClassName());
        if (classMetadata.getClassName().contains("MyService")){
            return true;
        }
        return false;
    }
}

测试结果如下:

myBeanPerson
myBea
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐