既然你精通SpringBoot,那给我说一下类的自动装配吧!

本文详细剖析了SpringBoot的自动配置机制,从@SpringBootApplication注解开始,解释了@SpringBootConfiguration、@ComponentScan和@EnableAutoConfiguration的作用。同时,文章介绍了以@Enable开头的注解用于开启特定功能,以及配置类、选择器和注册器的角色。最后,探讨了@EnableAutoConfiguration的解析过程和application.yml的加载机制。
摘要由CSDN通过智能技术生成

剖析@SpringBootApplication注解

创建一个SpringBoot工程后,SpringBoot会为用户提供一个Application类,该类负责项目的启动:

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

这是一个被 @SpringBootApplication 注解的类,该注解完成了SpringBoot中类的自动装配任务:

@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 {
    
}

抛却元注解不谈,@SpringBootApplication继承了三个注解:

@SpringBootConfiguration

/**
 * Indicates that a class provides Spring Boot application
 * {@link Configuration @Configuration}. Can be used as an
 * alternative to the Spring's standard @Configuration 
 * annotation so that configuration can be found
 * automatically (for example in tests).
 *
 * Application should only ever include one 
 * @SpringBootConfiguration and most idiomatic Spring Boot 
 * applications will inherit it from @SpringBootApplication.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
    ...
}

在说明中提到, @SpringBootConfiguration 注解是用来替代Spring的 @Configuration ,方便SpringBoot自动找到配置。

@ComponentScan

/**
 * Configures component scanning directives
 * for use with Configuration classes.
 * Provides support parallel with Spring XML's
 * <context:component-scan> element.
 *
 * Either #basePackageClasses or #basePackages
 * (or its alias #value} may be specified to
 * define specific packages to scan. If specific
 * packages are not defined, scanning will occur
 * from the package of the class that declares
 * this annotation.
 *
 * Note that the <context:component-scan> element
 * has an annotation-config attribute; however,
 * this annotation does not. This is because
 * in almost all cases when using @ComponentScan,
 * default annotation config processing
 * (e.g. processing @Autowired and friends)
 * is assumed. Furthermore, when using 
 * AnnotationConfigApplicationContext,
 * annotation config processors are always
 * registered, meaning that any attempt to disable
 * them at the @ComponentScan level would be ignored.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    ...
}

在说明中我们可以得知: @ComponentScan 只负责指定要扫描的包,并没有装配其中的类,这个真正装配这些类是 @EnableAutoConfiguration 。

@EnableAutoConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
    ...
}

该类真正完成了SpringBoot对于类的装配工作,具体内容在后续会作出解释。

以@Enable开头的注解

以@Enable开头的注解( @EnableXxx )一般用于开启某一项功能,是为了简化代码的导入。它是一个组合注解,一般情况下 @EnableXxx 注解中都会组合一个 @Import 注解,而该 @Import 注解用于导入指定的类,而被导入的类一般有三种:

Java学习交流群:1106441130  欢迎讨论交流,另外可免费领取一份(Java学习视频,技术文档,电子书籍,面试等资料...)

配置类

  • 类的特征:@Import中指定的类一般以Configuration结尾
  • 类的配置:该类上会注解@Configuration
  • 类的案例:定时任务启动注解: SchedulingConfiguration

    @Target(ElementType.TYPE)
    @Retention(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot自动装配原理是基于Spring框架的自动装配机制的基础上进行的。在Spring Boot中,通过@EnableAutoConfiguration注解来开启自动装配,该注解会自动扫描classpath下的所有jar包,查找META-INF/spring.factories配置文件,并根据配置文件中的内容,自动装配相应的依赖。 具体来Spring Boot自动装配原理的实现过程如下: 1. 在启动时,Spring Boot会自动加载所有在classpath下的jar包,并查找其中的META-INF/spring.factories配置文件。 2. 配置文件中定义了一些自动配置的全限定名,例如: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration 3. Spring Boot会根据配置文件中的定义,自动创建相应的自动配置,并根据需要的Bean进行自动装配。 4. 自动配置中会根据条件判断和配置文件中的属性值等,决定是否需要自动装配相应的Bean。 5. 如果需要自动装配Bean,自动配置会通过Spring框架的IoC容器来创建Bean,并注入到需要的地方。 通过自动装配Spring Boot可以快速创建一个可用的Web应用程序,并且无需手动配置大量的依赖关系。这使得开发者可以更加专注于业务逻辑的实现,而不必花费太多的时间和精力去处理依赖关系的配置问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值