4

I am reading property files using below entry in my Spring xml.

<context:property-placeholder 
    location="classpath:resources/database1.properties,
              classpath:resources/licence.properties"/>

I am injecting this values in variable using xml entry or using @Value annotation.

<bean id="myClass" class="MyClass">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="name" value="${database.name}" />
</bean>

I want to add a new property file(database2.properties) which has few same variable names as of database1.properties.

database1.properties:

database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://192.168.1.10/
database.name=dbname

database2.properties:

database.url=jdbc:mysql://192.168.1.50/
database.name=anotherdbname
database.user=sampleuser

You can see few property variables have same name like database.url, database.name in both the property files.

Is it possible to inject database.url of database2.properties?

Or I have to change variable names?

Thank you.

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55

3 Answers3

3

There might be a solution, similar to what's that you want to achieve. Check the second answer to this question: Multiple properties access. It basically explains what to do in order to access the properties of the second file by using another expression, which is defined by you. Otherwise, the simplest solution would be just changing the key values (the variable names).

Community
  • 1
  • 1
Endrik
  • 2,238
  • 3
  • 19
  • 33
3

You can do it by configuring two PropertyPlaceholderConfigurer. Usually there's only one instance that serves out all the properties, however, if you change the placeholderPrefix you can use two instances, something like

<bean id="firstPropertyGroup" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:resources/database1.properties,
              classpath:resources/licence.properties" />
   <property name="placeholderPrefix" value="${db1."/>
</bean>

<bean id="secondPropertyGroup" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:resources/database2.properties" />
  <property name="placeholderPrefix" value="${db2."/>"
</bean>

Then you would access your properties like ${db1.database.url} or ${db2.database.url}

Master Slave
  • 27,771
  • 4
  • 57
  • 55
2

You will sooner or later switch to Spring Boot. So with Spring Boot you can do have such POJO:

public class Database {

  @NotBlank
  private String driver;
  @NotBlank
  private String url;
  @NotBlank
  private String dbname;

  public String getDriver() {
    return driver;
  }
  public void setDriver(String driver) {
    this.driver = driver;
  }
  public String getUrl() {
    return url;
  }
  public void setUrl(String url) {
    this.url = url;
  }
  public String getDbname() {
    return dbname;
  }
  public void setDbname(String dbname) {
    this.dbname = dbname;
  }
}

and use @ConfigurationProperties to fill it:

@Bean
@ConfigurationProperties(locations="classpath:database1.properties", prefix="driver")
public Database database1(){
  return new Database();
}

@Bean
@ConfigurationProperties(locations="classpath:database2.properties", prefix="driver")
public Database database2(){
  return new Database();
}

Downside of this is that it's mutable. With Lombok library, you can eliminate nasty getters and setters.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • 1
    +1 Thanks for answering. For now I am not using `Spring Boot`, so will not be able to use your solution.. – Naman Gala Apr 16 '15 at 09:57