一、初始化–配置阶段
Spring Boot配置阶段位于构造器阶段和运行阶段之间,该阶段是可以选择的,主要用于调整或者补充构造阶段的状态、左右运行时行:
- SpringApplication Setter方法为代表,用于调整SpringApplication的行为
- 补充行为则以add*方法为主
此外通过Setter配置行为过于繁琐,因此,Spring Boot引入了SpringApplicationBuilder以提升API的便利性。
二、自定义SpringApplication
(1)调整SpringApplication配置
以banner关闭为例:
@SpringBootApplication
public class SimpleApplication2 {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
// 设置关闭banner
app.setBannerMode(Banner.Mode.CONSOLE);
app.run(args);
}
}
@EnableAutoConfiguration
class MySpringConfiguration {
}
(2)SpringApplication & SpringApplicationBuider 对比
// 1. SpringApplication setter 方式
class SimpleApplication3 {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(MySpringConfiguration.class);
springApplication.setBannerMode(Banner.Mode.CONSOLE);
springApplication.setWebApplicationType(WebApplicationType.NONE);
springApplication.setAdditionalProfiles("prod");
springApplication.setHeadless(true);
springApplication.run(args);
}
}
// 2. SpringApplicationBuider方式
class SimpleApplication4 {
public static void main(String[] args) {
new SpringApplicationBuilder(MySpringConfiguration.class)
.bannerMode(Banner.Mode.CONSOLE)
.web(WebApplicationType.NONE)
.profiles("prod")
.headless(true)
.run(args);
}
}
(3)SpringApplication配置源
SpringBoot 1.x 与SpringBoot 2.x对比,1.x中不区分主配置源,而2.x中通常将引导类作为主配置源,所以SpringApplication在前后版本中的使用方式是相同的,并且运行时也无法察觉其中变化,其主要原因在于1.x中的主配置以SpringApplication.sources属性存储:
// SpringBoot 1.x
public class SpringApplication {
...
private final Set<Object> sources = new LinkedHashSet<Object>();
...
public SpringApplication(Object... sources) {
initialize(sources);
}
private void initialize(Object[] sources) {
if (sources != null && sources.length > 0) {
this.sources.addAll(Arrays.asList(sources));
}
...
}
}
// SpringBoot 2.x 未移除sources
public class SpringApplication {
...
// 主配置源
private Set<Class<?>> primarySources;
// 1.x 配置源
private final Set<Object> sources = new LinkedHashSet<Object>();
...
public SpringApplication(Object... sources) {
initialize(sources);
}
public SpringApplication(Class<?>... primarySources) {
this(null, primarySources);
}
// 初始化主配置源
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
...
}
// 初始化其他配置源
private void prepareContext(ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
...
// Load the sources
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
listeners.contextLoaded(context);
}
}
Spring Boot 2.x 支持配置源:
- 主配置类
- @Configuration Class
- XML配置文件
- package
(4)调整Spring Boot外部化配置
application.properties 是典型的外部化配置文件,实际上它可覆springApplication.setDefaultProperties(); 而反之springApplication.setDefaultProperties();也能通过spring.config.location 来定义application.properties的搜索路径;