Spring expressions doesn't work inside the PropertySource annotation.
@PropertySource({ "classpath:application.properties",
"#{systemProperties['user.home']}/.app.properties" })
@Configuration
public class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer result = new PropertySourcesPlaceholderConfigurer();
result.setOrder(0);
return result;
}
}
When We define property files using “classpath” as shown in above examples, it searches that file at project classpath and resolve all values. To define multiple properties file @Configuration. @PropertySource(value={"classpath:default.properties","classpath:config.properties"})
Spring @PropertySource annotation is used to provide properties file to Spring Environment. This annotation is used with @Configuration classes. Spring PropertySource annotation is repeatable, means you can have multiple PropertySource on a Configuration class.
@PropertySource is a convenient annotation for including PropertySource to Spring's Environment and allowing to inject properties via @Value into class attributes. ( PropertySource is an object representing a set of property pairs from a particular source.) @PropertySource is used together with @Configuration .
You can directly use file:${user.home}
to load a file under user home:
@PropertySource({ "classpath:application.properties",
"file:${user.home}/.app.properties" })
@Configuration
public class Config {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With