javascript
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 beanscontext.getBeanFactory().registerSingleton("springApplicationArguments",applicationArguments);if (printedBanner != null) {context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);}// Load the sourcesSet<Object> sources = getSources();//獲取需要加載的class源Assert.notEmpty(sources, "Sources must not be empty");load(context, sources.toArray(new Object[sources.size()]));//加載classlisteners.contextLoaded(context);} -
ApplicationListener簡介
org.springframework.context.ApplicationListener會在SpringBoot中幾個典型事件產生后調用onApplicationEvent方法。詳見Spring事件發布系統。 -
Autoconfiguration簡介
org.springframework.boot.autoconfigure.EnableAutoConfiguration是SpringBoot中最常見配置,用于自動配置。只要有@EnableAutoConfiguration注解,該配置的所有類都會自動加載到Spring容器中。
沒有智能的代碼,源碼面前了無秘密
總結
以上是生活随笔為你收集整理的SpringBoot常用配置简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring 的@Bean 的用法
- 下一篇: SpringCloud的版本