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