你可以在对ApplicationEnvironmentPrepared事件作出反应的生命周期侦听器中添加其他属性源。
类似于以下内容:
public class DatabasePropertiesListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); Properties props = new Properties(); props.put("spring.datasource.url", "<my value>"); environment.getPropertySources().addFirst(new PropertiesPropertySource("myProps", props)); }}然后在src / main / resources / meta-INF / spring.factories中注册该类:
org.springframework.context.ApplicationListener=my.package.DatabasePropertiesListener
这对我有用,但是,由于在应用程序启动阶段还处于初期,你在此时只能做些限制,因此你必须找到一种无需依赖其他弹簧即可获取所需值的方法



