zoukankan      html  css  js  c++  java
  • SpringBoot常用配置简介

    SpringBoot常用配置简介

    1. SpringBoot中几个常用的配置的简单介绍

    • 一个简单的Spring.factories

       # Bootstrap components
       org.springframework.cloud.bootstrap.BootstrapConfiguration=
       org.springframework.cloud.config.server.bootstrap.ConfigServerBootstrapConfiguration
      
       # Application listeners
       org.springframework.context.ApplicationListener=
       org.springframework.cloud.config.server.bootstrap.ConfigServerBootstrapApplicationListener      
       # Autoconfiguration
       org.springframework.boot.autoconfigure.EnableAutoConfiguration=
       org.springframework.cloud.config.server.config.EncryptionAutoConfiguration,
       org.springframework.cloud.config.server.config.SingleEncryptorAutoConfiguration
      
    • BootstrapConfiguration简介
      Spring.factories中的配置项, org.springframework.cloud.bootstrap.BootstrapConfiguration,会在Spring启动之前的准备上下文阶段将其加入到容器中。我们在介绍 @Configuration配置解析一文中曾详细解释Configuration中的解析。BootstrapConfiguration是在刷新(refresh)阶段将class作为预选的配置类@Configuration注入到容器中的,在@Configuration配置解析阶段会解析此class。

    • 加载BootstrapConfiguration配置的类

    List<String> names = SpringFactoriesLoader
    				.loadFactoryNames(BootstrapConfiguration.class, classLoader);
    		for (String name : StringUtils.commaDelimitedListToStringArray(
    				environment.getProperty("spring.cloud.bootstrap.sources", ""))) {
    			names.add(name);
    		}
    		// TODO: is it possible or sensible to share a ResourceLoader?
    		SpringApplicationBuilder builder = new SpringApplicationBuilder()
    				.profiles(environment.getActiveProfiles()).bannerMode(Mode.OFF)
    				.environment(bootstrapEnvironment)
    				.properties("spring.application.name:" + configName)
    				.registerShutdownHook(false).logStartupInfo(false).web(false);
    		List<Class<?>> sources = new ArrayList<>();
    		for (String name : names) {
    			Class<?> cls = ClassUtils.resolveClassName(name, null);
    			try {
    				cls.getDeclaredAnnotations();
    			}
    			catch (Exception e) {
    				continue;
    			}
    			sources.add(cls);
    		}
    		builder.sources(sources.toArray(new Class[sources.size()]));
    		AnnotationAwareOrderComparator.sort(sources);
    		final ConfigurableApplicationContext context = builder.run();
    
    • 加载BootstrapConfiguration配置的类
    private void prepareContext(ConfigurableApplicationContext context,ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,ApplicationArguments applicationArguments, Banner printedBanner) {
    		context.setEnvironment(environment);
    		postProcessApplicationContext(context);
    		applyInitializers(context);
    		listeners.contextPrepared(context);
    		if (this.logStartupInfo) {
    			logStartupInfo(context.getParent() == null);
    			logStartupProfileInfo(context);
    		}
        // Add boot specific singleton beans
    		context.getBeanFactory().registerSingleton("springApplicationArguments",
    				applicationArguments);
    		if (printedBanner != null) {
    			context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);
    		}
    
    		// Load the sources
    		Set<Object> sources = getSources();//获取需要加载的class源
    		Assert.notEmpty(sources, "Sources must not be empty");
    		load(context, sources.toArray(new Object[sources.size()]));//加载class
    		listeners.contextLoaded(context);
          }
    
    • ApplicationListener简介
      org.springframework.context.ApplicationListener会在SpringBoot中几个典型事件产生后调用onApplicationEvent方法。详见Spring事件发布系统

    • Autoconfiguration简介
      org.springframework.boot.autoconfigure.EnableAutoConfiguration是SpringBoot中最常见配置,用于自动配置。只要有@EnableAutoConfiguration注解,该配置的所有类都会自动加载到Spring容器中。

  • 相关阅读:
    记一个SharePoint Workflow一睡永不醒来的问题
    .NET Tracing简介
    !aspxpages(即!dumphttpcontext)命令输出的解释
    MOSS定时爬网无法启动的问题
    Persistent cookies和Session cookies的定义与区别
    跨SharePoint服务器场的Content Deployment的一个知识点
    如何明确指定命令所在的debugger extension?
    理解SharePoint中的备用访问映射(Alternate Access Mapping)
    biztalk中ACK、NACK详测示例【转】
    深入biztalk消息以及消息订阅发布路由机制(一)-消息概述【转】
  • 原文地址:https://www.cnblogs.com/dragonfei/p/6060419.html
Copyright © 2011-2022 走看看