Spring Boot 使用 PropertySource 顺序,旨在允许合理地覆盖值,属性按以下顺序考虑:
但我不喜欢这个。我怎样才能改变它?
我找到了实现这一目标的方法。开源!!!
App.java(主方法)
public class App {
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(AppConfig.class);
SpringApplication app = builder.web(true).listeners(new AppListener()).build(args);
app.run();
}
}
Run Code Online (Sandbox Code Playgroud)
应用监听器.java
public class AppListener implements GenericApplicationListener {
public static final String APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME = "applicationConfigurationProperties";
@Override
public boolean supportsEventType(ResolvableType eventType) {
return ApplicationPreparedEvent.class.getTypeName().equals(eventType.getType().getTypeName());
}
@Override
public boolean supportsSourceType(Class<?> sourceType) {
return true;
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationPreparedEvent) {
ApplicationPreparedEvent _event = (ApplicationPreparedEvent) event;
ConfigurableEnvironment env = _event.getApplicationContext().getEnvironment();
// change priority order application.properties in PropertySources
PropertySource ps = env.getPropertySources().remove(APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME);
env.getPropertySources().addFirst(ps);
// logging.config is my testing property. VM parameter -Dlogging.config=xxx will be override by application.properties
System.out.println(env.getProperty("logging.config"));
}
}
@Override
public int getOrder() {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7443 次 |
| 最近记录: |