Reputation: 49
I want to perform something similar to the following code using Spring
class MyPropotypeBean {
/* We can not for static file name like
* @ConfigurationProperties(prefix = "abcd", locations = "file:conf/a2z.properties")
*/
public MyPropotypeBean(String propFileLocation) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(propFileLocation);
prop.load(input);
gMapReportUrl = prop.getProperty("gMapReportUrl");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
...
}
}
}
I want propFileLocation dynamically injected something similar to the following.
@ConfigurationProperties(prefix = "abcd", locations = "file:conf/" + propFileLocation + ".properties")
I know we cannot achieve using annotation. How Can I do it pragmatically?
Upvotes: 0
Views: 1977
Reputation: 175
You can use Spring ResourceBundleMessageSource
. It helps to load external properties. Have a look at here.
@Value("${file.directory}")
private String propFileLocation;
//getters and setters
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("file:conf/"+propFileLocation);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
Upvotes: 1