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容器中。

SpringBoot常用配置简介的更多相关文章

  1. [转]logback常用配置简介

    logback是一套日志框架,由log4j的优化版,由同一个作者开发,在速度和性能上都超过其他日志框架,再结合slf4j,已成为当前最流行的日志框架. Logback最常用就是在classpath定义 ...

  2. SpringBoot常用配置,引入外部配置文件信息,热加载

    SpringBoot的配置文件格式 yml规范 SpringBoot的配置文件支持properties和yml,甚至还支持json. 更推荐使用yml文件格式: yml文件,会根据换行和缩进帮助咱们管 ...

  3. SpringBoot常用配置

    前言:springboot集成了主流的第三方框架,但是需要使用springboot那一套配置方式.但是我这里只列举了非常非常常用的,可以看已发的几篇博客,慢慢会补充.当然官方文档里也有相应的配置,可惜 ...

  4. ActiveMQ (二) 常用配置简介

    ActiveMQ的主要配置文件 ActiveMQ的一些常用的属性很多可以在对应的配置文件中进行配置的.比如访问web console的管理端的端口,用户名密码,连接MQ时的用户名和密码,持久化设置,是 ...

  5. SpringBoot 常用配置 静态资源访问配置/内置tomcat虚拟文件映射路径

    Springboot 再模板引擎中引入Js等文件,出现服务器拒绝访问的错误,需要配置过滤器 静态资源访问配置 @Configuration @EnableWebMvc public class Sta ...

  6. 【转】logback 常用配置详解(序)logback 简介

    原创文章,转载请指明出处:http://aub.iteye.com/blog/1101222, 尊重他人即尊重自己 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透 ...

  7. logback常用配置详解及logback简介

    logback 简介(一) Ceki Gülcü在Java日志领域世界知名.他创造了Log4J ,这个最早的Java日志框架即便在JRE内置日志功能的竞争下仍然非常流行.随后他又着手实现SLF4J 这 ...

  8. SpringBoot阿里巴巴Fastjson的一些常用配置

    SpringBoot阿里巴巴Fastjson的一些常用配置 @Bean public HttpMessageConverters fastJsonHttpMessageConverters() { F ...

  9. SpringBoot 入门篇(二) SpringBoot常用注解以及自动配置

    一.SpringBoot常用注解二.SpringBoot自动配置机制SpringBoot版本:1.5.13.RELEASE 对应官方文档链接:https://docs.spring.io/spring ...

随机推荐

  1. MySQL入门手册

    本文内容摘自MySQL5.6官方文档,主要选取了在实践过程中所用到的部分文字解释,力求只摘录重点,快速学会使用MySQL,本文所贴代码地方就是我亲自练习过的代码,凡本文没有练习过的代码都没有贴在此处, ...

  2. Sublime+Golang Plugin

    很喜欢在Sublime开发Golang程序,主要是要一个Sublime Golang Plugin, 可以给代码autocomplement.相当的棒! 1.安装Sublime https://www ...

  3. 用sql语句清除日志

    DUMP TRANSACTION [数据库] WITH NO_LOGBACKUP LOG [数据库] WITH NO_LOGDBCC SHRINKDATABASE([数据库])

  4. Java生成不重复的数的方法

    在开发时要给某些表加上编号,而且编号是唯一的,自己用时间生成了下,觉得可能存在并发情况.所以在网上查了一下,就是随机生成.方法如下: //方法一(用当前时间精确到毫秒,截取任意几位) Date dat ...

  5. IIS7 应用程序池回收

    原文:http://technet.microsoft.com/zh-cn/library/cc754494 应用到: Windows 7, Windows Server 2008, Windows ...

  6. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  7. poi解析excel 03、07

    maven依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...

  8. 读bootstrap2.3.2有感1

    起步: 下载编译好的bootstrap2文件,百度新版jquery.js,并复制html模版(hello world)放置在同一目录,然后看了下官网上的范例网站,心里还是很激动啊~ <!DOCT ...

  9. JAVA 正则表达式4种常用的功能

    下面简单的说下它的4种常用功能:   查询:   以下是代码片段: String str="abc efg ABC";    String regEx="a|f" ...

  10. HTML5-表单的创建

    <!-- 文本-->, <!-- 复选框-->,<!-- 单选按钮-->, <!-- 下拉菜单,select:选择,option:选项--> <! ...