SpringBootApplication是springboot启动类注解,是一个组合注解除了元注解外,主要包含三个注解;
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan
一、@SpringBootConfiguration
其中@SpringBootConfiguration其实就是springboot对spring的@Configuration的再封装。
二、@ComponentScan
@ComponentScan是配置用于@Configuration类的组件包扫描的注解。可以指定basepackageclass或basePackages(或其别名值)来定义要扫描的特定包。如果没有指定特定的包,则将从声明该注释的类的包中进行扫描。
三、@EnableAutoConfiguration
@EnableAutoConfiguration来决定启用SpringBoot上下文的自动配置的注解。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
/**
* Environment property that can be used to override when auto-configuration is
* enabled.
*/
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
String[] excludeName() default {};
}
其中@AutoConfigurationPackage 是用来指定自己实现的自动配置的包路径,如果没有指定基包或基包类,则注册带注释的类的包以及子包。
@Import(AutoConfigurationImportSelector.class)中,@Import表示导入bean 这里表示导入AutoConfigurationImportSelector这个类
AutoConfigurationImportSelector类中,下面的方法返回应该考虑的自动配置类名。



会拿到META-INF/spring.factories里面的配置来加载所指定的类,该类注入相关的bean到spring容器,从而实现自动配置。

本文详细解读了SpringBoot中的@SpringBootConfiguration、@ComponentScan和@EnableAutoConfiguration注解的作用,以及它们在自动配置过程中的角色。
953

被折叠的 条评论
为什么被折叠?



