Spring Boot 2014正式发布1.0版本,距今已经快10年了。看历史官方文档了解重点feature, 帮助自己建立知识网络。
与 Spring 5 官网历史文档学习 一样,尽量保证不误解文档作者的原意,不好翻译的会有原文摘录(包括一些专有名词),并辅以自己的理解。限于篇幅原因,只摘录工作中遇到过的或者是有兴趣的。
Spring Boot 基础知识,排错或需要拓展Spring Boot的时候要考虑到这个
在 ApplicationContext 创建完成之前,会依次发生以下事件:
实际开发中可能不会用到这些事件,但是Spring Boot借助这些事件完成了许多功能。
You often won’t need to use application events, but it can be handy to know that they exist. Internally, Spring Boot uses events to handle a variety of tasks.
实现一套代码在不同的环境部署。实现外部化配置的方法:
外部化配置的元素还能注入到bean,通过
@ValueEnvironment 抽象PropertySource 读取配置的顺序,数字大的覆盖数字小的:
System.getProperties()@Configuration 标注的类,又由 @PropertySource 注入SpringApplication.setDefaultProperties实际应用:自己的jar包提供默认实现(自己是个starter),生产项目依赖这个starter,运行生产项目覆盖这个配置
值得注意的是配置文件的读取优先级
Spring Boot 1.0 yml 文件的缺点
@PropertySource 没法读到 YAML 的配置@ConfigurationProperties(name="connection") 的使用
connection:
username: admin
remoteAddress: 192.168.1.1
@Component
@ConfigurationProperties(name="connection")
public class ConnectionSettings {
private String username;
@NotNull
private InetAddress remoteAddress;
// ... getters and setters
}