SpringBoot-启动配置分析

版本:

  在pom文件中,顺着 <artifactId>spring-boot-starter-web</artifactId> 一直向上找,最终可以看到所有依赖包的版本管理:

<properties>
        <activemq.version>5.15.3</activemq.version>
        <antlr2.version>2.7.7</antlr2.version>
        <appengine-sdk.version>1.9.63</appengine-sdk.version>
        <artemis.version>2.4.0</artemis.version>
        <aspectj.version>1.8.13</aspectj.version>
        <assertj.version>3.9.1</assertj.version>
        ......
</properties>
View Code

引入依赖:

  在官网的文档中,搜索starters,可以看到各种启动需要的依赖模块,如果需要使用某一项,点击右侧的pom连接,复制其中的<artifactId>...</artifactId>部分,放在项目的pom中即可。

启动初始化:

  从run方法一路跟踪,可以看到启动的时候对很多参数进行了初始化,包括上节的(AnnotationConfigApplicationContext)

public class SpringApplication {
    public static final String DEFAULT_CONTEXT_CLASS = "org.springframework.context.annotation.AnnotationConfigApplicationContext";
    public static final String DEFAULT_WEB_CONTEXT_CLASS = "org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext";
    private static final String[] WEB_ENVIRONMENT_CLASSES = new String[]{"javax.servlet.Servlet", "org.springframework.web.context.ConfigurableWebApplicationContext"};
    public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext";
    private static final String REACTIVE_WEB_ENVIRONMENT_CLASS = "org.springframework.web.reactive.DispatcherHandler";
    private static final String MVC_WEB_ENVIRONMENT_CLASS = "org.springframework.web.servlet.DispatcherServlet";
    public static final String BANNER_LOCATION_PROPERTY_VALUE = "banner.txt";
    public static final String BANNER_LOCATION_PROPERTY = "spring.banner.location";
    private static final String SYSTEM_PROPERTY_JAVA_AWT_HEADLESS = "java.awt.headless";
    private static final Log logger = LogFactory.getLog(SpringApplication.class);
    private Set<Class<?>> primarySources;
    private Set<String> sources;
    private Class<?> mainApplicationClass;
    private Mode bannerMode;
    private boolean logStartupInfo;
    private boolean addCommandLineProperties;
    private Banner banner;
    private ResourceLoader resourceLoader;
    private BeanNameGenerator beanNameGenerator;
    private ConfigurableEnvironment environment;
    private Class<? extends ConfigurableApplicationContext> applicationContextClass;
    private WebApplicationType webApplicationType;
    private boolean headless;
    private boolean registerShutdownHook;
    private List<ApplicationContextInitializer<?>> initializers;
    private List<ApplicationListener<?>> listeners;
    private Map<String, Object> defaultProperties;
    private Set<String> additionalProfiles;
    
    public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
        this.sources = new LinkedHashSet();
        this.bannerMode = Mode.CONSOLE;
        this.logStartupInfo = true;
        this.addCommandLineProperties = true;
        this.headless = true;
        this.registerShutdownHook = true;
        this.additionalProfiles = new HashSet();
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
        this.webApplicationType = this.deduceWebApplicationType();
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
        this.mainApplicationClass = this.deduceMainApplicationClass();
    }
    
    ......
}
View Code

 @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 {
    ......
}
View Code

   @SpringBootConfiguration -->SpringBoot的配置类,标注在某个类上,表示这是一个SpringBoot的配置类。

  @Configuration -->配置文件,配置类也是容器中的一个组件;一路跟踪可以看到它由@Component修饰。

  @EnableAutoConfiguration -->开启自动配置功能。

  @Import(AutoConfigurationImportSelector.class) -->给容器中倒入组件,AutoConfigurationImportSelector将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中。可以在:List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);打断点查看。引入内容与org.springframework.boot:spring-boot-autoconfigure >> META-INF >> spring.factories中的auto config相同。

  比如从spring.factories列表中查看:WebMvcAutoConfiguration,里面的配置和以往xml中的配置是一样的。

  @AutoConfigurationPackage -->自动配置包。

  @Import(AutoConfigurationPackages.Registrar.class) -->Spring的底层注解,给容器中倒入一个组件,导入的组件由AutoConfigurationPackages.Registrar.class将主配置类(@SpringBootApplication)的所在包以及下面的所有子包里面的所有组件扫描到Spring容器,可以在Registrar里面打断点调试看出(选中new PackageImport(metadata).getPackageName(),右键 Evaluate Expression)

  

猜你喜欢

转载自www.cnblogs.com/wange/p/9769504.html
今日推荐