Spring注解02 @ComponentScan 自动扫描组件

前言

现今SpringBoot、SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解、原理,比如@Conditional、@Import、@EnableXXX等。
如果掌握这些底层原理、注解,那么我们对这些高层框架就能做到高度定制,使用的游刃有余;

本文涵盖Spring的常用注解
@ComponentScan、@Bean、@Configuration、@Conditional、@Import、@PropertySource、@Profile等

1.@Configuration

  • 1.1 配置类
@Configuration //告诉Spring这是一个配置类
public class MainConfig {
   //给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id
    @Bean(value = "person01")
    public Person person(){
        return new Person("jm",16,"tommy");
    }
}
  • 1.2 测试类
public class MainTest {

    public static void main(String[] args) {
        final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        final Person person = applicationContext.getBean(Person.class);
        System.out.println("person = [" + person + "]");
        final String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
        for (String name : namesForType) {
            System.out.println(name);
        }
    }
}
  • 1.3 运行结果

person = [Person(name=jm, age=16, nickName=tommy)]
person01
Process finished with exit code 0

  • 1.4 结果分析

person01
给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id,也可以指定

2 @ComponentScans @ComponentScan

包扫描、只要标注了@Controller、@Service、@Repository,@Component

  • 2.1 配置文件方式:

<context:component-scan base-package="com.tommy" use-default-filters="false"></context:component-scan>

  • 2.2 @ComponentScans方式
  • 2.2.1 配置类
    @ComponentScan value:指定要扫描的包
@Configuration
@ComponentScan(value = "com.tommy")
public class MainConfig {
    //给容器中注册一个bean;类型为返回值的类型,id默认是方法名作为id
    @Bean(value = "person")
    public Person person01() {
        return new Person("jm", 16, "tommy");
    }
}

运行结果:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
personController
personDao
personService
person

  • 2.2.2 excludeFilters
    excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件
@Configuration
@ComponentScan(value = "com.tommy",
        excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
                classes = {Controller.class, Service.class})
        })

运行结果:
....省略....
mainConfig
personDao
person

  • 2.2.3 includeFilters
    includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件
@Configuration
@ComponentScan(value = "com.tommy",
        includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
                classes = {Controller.class, Service.class})
        },useDefaultFilters = false )

运行结果:
.....省略.....
personController
personService
person

*注意:使用includeFilters 时,必须设置 useDefaultFilters = false,才能生效。

  • 2.2.4 @ComponentScans
@Configuration
@ComponentScans(value = {
        @ComponentScan(value = "com.tommy",
                excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
                        classes = {Controller.class, Service.class})
                }, useDefaultFilters = false)
})
  • 2.2.5 FilterType

FilterType.ANNOTATION:按照注解
FilterType.ASSIGNABLE_TYPE:按照给定的类型;
FilterType.ASPECTJ:使用ASPECTJ表达式
FilterType.REGEX:使用正则指定
FilterType.CUSTOM:使用自定义规则

  • 2.2.5.1 CUSTOM自定义规则
    配置类
@ComponentScan(value = "com.tommy",
        includeFilters = {
                @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
        }, useDefaultFilters = false)

MyTypeFilter .java

public class MyTypeFilter implements TypeFilter {

    /**
     * @param metadataReader        读取到的当前正在扫描的类的信息
     * @param metadataReaderFactory 可以获取到其他任何类信息的
     * @return
     * @throws IOException
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        final AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        final ClassMetadata classMetadata = metadataReader.getClassMetadata();
        final Resource resource = metadataReader.getResource();

        final String className = classMetadata.getClassName();
        if (className.contains("er")) {
            System.out.println("--->" + className);
            return true;
        }
        return false;
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 接上文,接着学习Spring的注解。本篇博客所学习注解基本都是spring-context对应jar包下的注...
    骑着乌龟去看海阅读 6,671评论 0 14
  • 前言 Spring 框架核心组件之一是 IOC,IOC 则管理 Bean 的创建和 Bean 之间的依赖注入,对于...
    faunjoe阅读 5,474评论 1 8
  • 2016.10.5 厦门 晴 同事搬去自己的住处,我开始对我的小窝大扫除,整理整理、清洗清洗,让它恢复了原来...
    简单木子阅读 988评论 0 0
  • 01 ▲管理学界曾经有过一次投票,想要针对管理理念选出最为重要的一条标准。 结果让人大跌眼镜,上榜的并非著名的“蓝...
    兜帅宫阅读 3,919评论 0 0
  • 昨天(2018.8.4)下午二点多至今天凌晨五点,一个离家出走的三年级的小男孩,让多少人煎熬着这十几个小时?好在孩...
    盛视微言阅读 1,872评论 0 0