Spring Boot 的启动类解析
-
@SpringBootApplication: Springboot启动类注解
- 其中,启动类的代码如下所示
package cn.lazyfennec.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloSpringbootApplication { public static void main(String[] args) { SpringApplication.run(HelloSpringbootApplication.class, args); } }
- @SpringBootApplication 的源码如下所示
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.boot.autoconfigure; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.context.TypeExcludeFilter; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.core.annotation.AliasFor; @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 { @AliasFor( annotation = EnableAutoConfiguration.class ) Class<?>[] exclude() default {}; @AliasFor( annotation = EnableAutoConfiguration.class ) String[] excludeName() default {}; @AliasFor( annotation = ComponentScan.class, attribute = "basePackages" ) String[] scanBasePackages() default {}; @AliasFor( annotation = ComponentScan.class, attribute = "basePackageClasses" ) Class<?>[] scanBasePackageClasses() default {}; @AliasFor( annotation = ComponentScan.class, attribute = "nameGenerator" ) Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; @AliasFor( annotation = Configuration.class ) boolean proxyBeanMethods() default true; }
可以看到@SpringBootApplication 这个注解中有三个重要的注解:
-
@SpringBootConfiguration,表示是Springboot的配置类--> Configuration--> 标明是一个配置类
-
@EnableAutoConfiguration:开启自动配置功能
- @AutoConfigurationPackage :自动配置包
- @Import(Register.class): 把启动类所在的包进行默认扫描。查看该Register类可以看到里边有个 static final class BasePackages ,通过打断点查看会发现,在没有做任何配置的情况下,packages 里边只有 xxxxApplcaition这个启动类所在的包,比如我们这里是 “cn.lazyfennec.springboot”,即表示,这种情况下,只有这个包(cn.lazyfennec.springboot)下的类、或者子包下的类会被自动扫描到,并加入到Spring的容器中进行管理
- @Import({AutoConfigurationImportSelector.class}):加载指定的类到Spring容器中去
- @Import(AutoConfigurationImportSelector.class):自动配置导入选择器,在该类中有方法“public Iterable<Entry> selectImports()”, 通过打断点发现有很多配置类的信息,如下所,表示根据项目来判断本项目需要哪些配置信息,然后把默认的配置内容导入到Spring容器中进行管理。
AutoConfigurationImportSelector selectImports()
- @AutoConfigurationPackage :自动配置包
-
@ComponentScan:组件扫描和自动装配,用来指定扫描容器的范围。
-
根据以上各个注解的关系可以作出如下的关系图
更多知识,请点击关注查看我的主页信息哦
网友评论