I am trying to create two classes to fetch configuration values from two different properties file (application-e1.properties and application-e2.properties).
application-e1.properties =>
param=e1Test
application-e2.properties =>
param=e2Test
I have created 2 classes EmployeeDev and EmployeeTest to read from respective files:
@Component
@PropertySource("classpath:application-e1.properties")
public class EmployeeDev {
@Value("${param}")
String param;
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
}
@Component
@PropertySource("classpath:application-e2.properties")
public class EmployeeTest {
@Value("${param}")
String param;
public String getParam() {
return param;
}
public void setParam(String param) {
this.param = param;
}
}
Whenever I call EmployeeDev or EmployeeTest, it is always returning param value as e2Test.
I am trying to create the configuration of 2 profiles at a time here. Can anyone help me through this?
According to Spring's Documentation this is what
@PropertySourcedoes:So all this annotation does is adding a specific property source to the existing environment.
What your code does is, it combines the two sources application-e1.properties and application-e2.properties by merging the properties into one environment, while the later properties overide the prior ones. You now have an
Environmentwhere param=e2Test is set. Adding already existing property sources are not predictably in terms of overriding.What you should do is having different
Profileswhich have their properties set in their related property files and then activate the profiles you need. If you wanna set differents specific params for different usecases within one and the same profile you have to name them separately.