我找到了一种方法来实现这一点。开源!
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();
}
}
AppListener.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;
}
}
来源
2016-12-03 15:09:39
Tan
你想改变什么? – Andreas
我想改变优先顺序。 – Tan
尚未找到解决方案,但您可以通过设计解决一些问题,尤其是在需要处理属性来源的特定顺序时。因此,对于应用程序设置,请始终使用自定义的@PropertySource,因为它会首先检查外部,然后检查内部(这样您就可以启动固定的默认设置并可以从外部文件中进行覆盖)。不要将设置与application.properties混合,因为9/10将在11之前匹配。 – DoNuT