Spring Boot 为这个早期处理步骤提供了一些不同的扩展点:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-boot-application.html#howto-customize-the-environment-or-application-context http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-boot-application.html#howto-customize-the-environment-or-application-context
在内部,这些选项是通过标准 Spring 的实现来实现的ApplicationContextInitializer
.
根据源的优先级,键/值将在environment.getProperty()
以及属性占位符。
因为这些是预配置上下文侦听器,所以没有其他可用的 bean,例如DataSource
。因此,如果应从数据库读取属性,则必须手动构建数据源和连接(最终是单独的数据源连接查找)。
选项:ApplicationEnvironmentPreparedEvent 的 ApplicationListener
构建一个应用程序监听器的实现ApplicationEnvironmentPreparedEvent
s and
将其注册在META-INF/spring.factories
和钥匙org.springframework.context.ApplicationListener
- or -
use the SpringApplicationBuilder
:
new SpringApplicationBuilder(App.class)
.listeners(new MyListener())
.run(args);
Example
public class MyListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
final ConfigurableEnvironment env = event.getEnvironment();
final Properties props = loadPropertiesFromDatabaseOrSo();
final PropertiesPropertySource source = new PropertiesPropertySource("myProps", props);
environment.getPropertySources().addFirst(source);
}
}
参考:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html#boot-features-application-events-and-listeners http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html#boot-features-application-events-and-listeners
选项:SpringApplicationRunListener
除了特殊事件之外,还有一个更通用的事件侦听器,其中包含多种类型事件的挂钩。
构建一个实现SpringApplicationRunListener
并将其注册到META-INF/spring.factories
和钥匙org.springframework.boot.SpringApplicationRunListener
.
Example
public class MyAppRunListener implements SpringApplicationRunListener {
// this constructor is required!
public MyAppRunListener(SpringApplication application, String... args) {}
@Override
public void environmentPrepared(final ConfigurableEnvironment environment) {
MutablePropertySources propertySources = environment.getPropertySources();
Properties props = loadPropertiesFromDatabaseOrSo();
PropertiesPropertySource propertySource = new PropertiesPropertySource("myProps", props);
propertySources.addFirst(propertySource);
}
// and some empty method stubs of the interface…
}
参考:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-boot-application.html#howto-customize-the-environment-or-application-context http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-boot-application.html#howto-customize-the-environment-or-application-context
选项:ApplicationContextInitializer
这是所有“非 Boot”Spring 开发人员的老朋友。然而,SpringApplication
首先模拟配置。
构建一个实现ApplicationContextInitializer
and
将其注册在META-INF/spring.factories
和钥匙org.springframework.context.ApplicationContextInitializer
.
- or -
use the SpringApplicationBuilder
:
new SpringApplicationBuilder(App.class)
.initializers(new MyContextInitializer())
.run(args);
Example
public class MyContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(final ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.getEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
Properties props = loadPropertiesFromDatabaseOrSo();
PropertiesPropertySource propertySource = new PropertiesPropertySource("myProps", props);
propertySources.addFirst(propertySource);
}
}