I have two properties file in a folder in file system. The path of this folder is passed using system properties -D=path/to/properties/folder
ex: java -jar -DpropDir=abc/def app.jar
These are properties files. Please note that both files have common key username,password.
mysql.properties
url=jdbc:mysql://localhost:3306
username=root
password=pass
vertica.properties
dburl=jdbc:vertica://123.123.123:4321/abcd
username=abcd
password=pass123
Now I want to access all these properties in respective classes. MySqlProperties.java and VerticaProperties.java like this.
@Component
public class VerticaProperties {
@Value("${dburl}")
private String dburl;
@Value("${username}")
private String username;
@Value("${password}")
private String password;
public String getDbUrl() {
return dburl;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
and similarly MySqlProperties.java
@Component
public class MySqlProperties {
@Value("${url}")
private String url;
@Value("${username}")
private String username;
@Value("${password}")
private String password;
public String getDbUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
But as key is same value is overriding for username and password.
How to access mysql.properties in MySqlProperties.java and vertica.properties in VerticaProperties.java classes.
PropertySource? - talexPropertyPlaceholderConfigurerwhich will add prefix to keys, but it will be easier to just add prefix manually to your properties. - talex