我注意到在启动期间使用的某些属性只能在application.properties.例如:src/main/java/foo/bar/Foo.java@SpringBootApplicationpublic class Foo { private static final Logger log = LoggerFactory.getLogger(Foo.class); public static void main(String... args) { ApplicationContext appContext = SpringApplication.run(Foo.class, args); log.info(appContext.getEnvironment().getProperty("spring.profiles.active")); }}src/main/resources/application.propertiesspring.profiles.active=dev控制台日志:09:23:48.827 : The following profiles are active: dev09:23:50.832 : dev该配置文件在启动时被识别为dev并在Environment. 这是预期的行为。但是,如果我将相同的属性从application.propertiesto移动foo.properties并加载为 a @PropertySource,则行为会发生变化。src/main/java/foo/bar/FooConfiguration.java@Configuration@PropertySource("classpath:foo.properties")public class FooConfiguration { }src/main/resources/foo.propertiesspring.profiles.active=prodsrc/main/resources/application.properties# empty控制台日志:09:35:18.141 : No active profile set, falling back to default profiles: default09:35:20.175 : prod该配置文件在启动期间不考虑,但在启动Environment后可用。问题:如何从加载属性@PropertySource并使它们在启动时在application.properties加载其余部分的同一点可用?
添加回答
举报
0/150
提交
取消