I have noticed there are certain properties used during startup that can only be set in the application.properties
.
For example:
src/main/java/foo/bar/Foo.java
@SpringBootApplication
public 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.properties
spring.profiles.active=dev
console logs:
09:23:48.827 : The following profiles are active: dev
09:23:50.832 : dev
The profile is recognized at startup as dev
and is available in the Environment
. This is the expected behavior.
However, if I move the same property from application.properties
to foo.properties
and load it as a @PropertySource
, the behavior changes.
src/main/java/foo/bar/FooConfiguration.java
@Configuration
@PropertySource("classpath:foo.properties")
public class FooConfiguration { }
src/main/resources/foo.properties
spring.profiles.active=prod
src/main/resources/application.properties
# empty
console logs:
09:35:18.141 : No active profile set, falling back to default profiles: default
09:35:20.175 : prod
The profile is not considered during startup, but is available on the Environment
after startup.
Question: How do I load properties from @PropertySource
and make them available at startup at the same point the rest of the application.properties
are loaded?
我认为您可以使用“ --spring.config.location”属性来指定要使用的属性文件。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.