SpringBoot的启动类@SpringBootApplication详解:

SpringBoot启动类:

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

@SpringBootApplication是一个组合注解,其中包含

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

1、查看SpringBootApplication中的参数

前面四个注解:(前面两个为元注解)

//元注解:对注解进行描述的注解,加在注解类上
@Target(ElementType.TYPE)
//有效时间,定义注解,一定:runtime
@Retention(RetentionPolicy.RUNTIME)
//注解是否在javaDoc文档显示
@Documented
//注解是否被继承,子类能不能继承这个父类的注解
@Inherited

 后面三个注解:

//springboot的配置类
@SpringBootConfiguration
//开启自动配置
@EnableAutoConfiguration
//扫描注解
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

2、查看@SpringBootConfiguration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration  //就是Configuration注解
public @interface SpringBootConfiguration {

3、查看@EnableAutoConfiguration

  @EnableAutoConfiguration 简单概括一下就是,借助@Import的支持,收集和注册特定场景相关的bean定义。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class) //导入一个类的核心类
public @interface EnableAutoConfiguration {

  借助AutoConfigurationImportSelector,@EnableAutoConfiguration可以

帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot创建并使用的IoC容器

AutoConfigurationImportSelector的核心方法:

@Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        }
        AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
                .loadMetadata(this.beanClassLoader);
        AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(autoConfigurationMetadata,
                annotationMetadata);
        return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
    }

 自动配置幕后英雄:SpringFactoriesLoader详解
       SpringFactoriesLoader属于Spring框架私有的一种扩展方案,其主要功能就是从指定的配置文件META-INF/spring.factories加载配置。

public abstract class SpringFactoriesLoader {

    private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);
    public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";

 配合@EnableAutoConfiguration使用的话,它更多是提供一种配置查找的功能支持,即根据@EnableAutoConfiguration的完整类名org.springframework.boot.autoconfigure.EnableAutoConfiguration作为查找的Key,获取对应的一组@Configuration类

 所以,@EnableAutoConfiguration自动配置:

配置类:    “spring04.config.JdbcConfig”   -->    反射技术  

               Class.forName()   --> JdbcConfig的Class对象

               --> newInstace()  -->  得到配置类的对象

               --> 配置类对象调用方法, 把方法产生的对象保存到Ioc容器

  selectImport()解析”MATA-INF/spring.factories” 文件, 这个文件包含所有的配置类的”包.类”, 通过反射,调用每一个配置类的方法, 这个方法上都有一个@Bean, 把这个方法产生的对象保存到IOC容器,  这个配置类的bean就是原先在spring配置文件中配置的bean

4、@ComponentScan包扫描

@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

component是组件,scan是扫描,所以这个注解的含义就是用来扫描组件的,

componentScan 扫描当前包及其子包下被 @Component@Controller@Service@Repository注解标记的类并纳入到spring容器中进行管理,所以这个注解会自动注入所有在主程序所在包下的组件。默认把当前启动类所在的包作为扫描包的起点,例如咱们的项目,扫描 com.maweiqi 包

以前在ssm项目中我们需要去配置我们的包扫描

<context:component-scan base-package="com.xxx"></context:component-scan>

 

posted @ 2020-07-04 10:21  64Byte  阅读(1175)  评论(0编辑  收藏  举报