How to load spring @PropertySource at startup

Zack :

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?

ipave :

I think you can use "--spring.config.location" property to specify what property file you want to use.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=153275&siteId=1