spring boot 自动扫描Controller、Service、Component原理

项目里面为什么不加上@ComponentScan("com.yym.*")注解,也能加载到子目录里面的Controller,Service,Component的bean呢?

启动类没有@ComponentScan注解

@SpringBootApplication
public class BootStrap {
    public static void main(String[] args) {
        SpringApplication.run(BootStrap.class, args);
    }
}

原因:

spring boot 启动类加上@SpringBootApplication会自动扫描当前目录,及子目录下的Controller,Service,Component注解的bean。

查看@SpringBootApplication注解源码,里面有@ComponentScan注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication

查看Controller,Service注解源码都有Component注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any (or empty String otherwise)
	 */
	@AliasFor(annotation = Component.class)
	String value() default "";

}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any (or empty String otherwise)
	 */
	@AliasFor(annotation = Component.class)
	String value() default "";

}

原理:

##AnnotationConfigServletWebServerApplicationContext构造器初始化AnnotatedBeanDefinitionReader、ClassPathBeanDefinitionScanner

##AnnotatedBeanDefinitionReader构造器初始化调用AnnotationConfigUtils.registerAnnotationConfigProcessors静态方法注册ConfigurationClassPostProcessor.class

##注册ConfigurationClassPostProcessor.class的BeanDefinition

##解析启动配置类

##看到ConfigurationClassParser解析ComponentScans.class, ComponentScan.class注解

##ComponentScanAnnotationParser的parse方法解析

##包名为空,添加启动类所在的包

##找到启动类所在包,及子包所有的bean候选者

##至此类被扫描成BeanDefinition并注册到DefaultListableBeanFactory的beanDefinitionMap

相关推荐

  1. SpringBoot 自动配置原理

    2024-01-10 06:18:03       36 阅读
  2. springboot 自动装载原理

    2024-01-10 06:18:03       19 阅读
  3. Springboot自动配置原理

    2024-01-10 06:18:03       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 06:18:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 06:18:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 06:18:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 06:18:03       20 阅读

热门阅读

  1. Linux和windows进程同步与线程同步那些事儿(一)

    2024-01-10 06:18:03       28 阅读
  2. Ubuntu and Mac OSX之间传输文件(共享文件夹方法)

    2024-01-10 06:18:03       41 阅读
  3. Python数据类型转换

    2024-01-10 06:18:03       35 阅读
  4. #{}和${}的区别?

    2024-01-10 06:18:03       23 阅读
  5. 离线安装docker和docker-compose

    2024-01-10 06:18:03       40 阅读
  6. 深度学习中Epoch和Batch Size的关系

    2024-01-10 06:18:03       34 阅读
  7. 树莓派Debian系统中如何用mDNS广播自己的ip地址

    2024-01-10 06:18:03       33 阅读
  8. [力扣 Hot100]Day1 两数之和

    2024-01-10 06:18:03       41 阅读