I have application.properties file:
value=a
Then I would like to load a property file based on that value - a.properties and read/use properties from that file.
I was thinking about something like this:
@Configuration
public class PropertiesConfiguration {
@Value("${value}")
private String value;
@Bean
public PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource(value + ".properties"));
return configurer;
} }
but the value is always null for some reason. When I try to get that value eg in service/component then it works fine. I would like to avoid using spring profiles. Any ideas how to achieve that with latest Spring?
In the line configurer.setLocation(new ClassPathResource(value + ".properties"));
it should be "application.properties"
since your file name is application.properties
Also in the properties file define it as value = a
with spaces
One of the solutions I have come across and works fine is use of Component and PropertySource annotations.
@Component
@PropertySource(value = "classpath:${value}.properties")
public class CountryService implements ICountryService {
@Value("${<whatever in a.properties file>}")
private String currency;
@Override
public String getCurrency() {
return currency;
}
}
where ${value} is taken from application.properties. Then Autowire that bean whenever needed.
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.