1

I've got two properties files:

environment.properties:
 - project.host.db3.database.name=oracle

application.properties:
 - database.name=${project.host.db3.database.name}

The first one represents the environment variables and the second one the properties to be used in a spring project, in this configuration i try to set the environment.properties but of course it doesn't work:

<bean id="systemPropertiesLoader"   
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" value="#{@systemProperties}" />
        <property name="targetMethod" value="putAll" />
        <property name="arguments">
    <util:properties location="classpath:environment.properties" />
</property>
</bean>

<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
depends-on="systemPropertiesLoader">
<property name="locations">
    <list>
        <value>classpath:application.properties</value>
    </list>
</property>

<!-- bean using database.name -->

Is it doable?, and if not, how do people have agnostic properties in their projects (like database.name), and only one file (war, jar, etc.) to be deployed?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user311174
  • 1,738
  • 1
  • 18
  • 17
  • Why can't you just use environment.properties directly? – yorkw Aug 07 '12 at 21:57
  • @yorkw, i started using my own properties and learnt afterwards i should feed them from another properties file, still i think i should isolate the name of my properties from the properties of environments – user311174 Aug 08 '12 at 06:36

1 Answers1

0

Well, it seems it's doable for beans xml defined as long as you define your properties it like this:

<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
depends-on="systemPropertiesLoader">

But if you ever try to access the properties from a servlet:

this.getClass().getClassLoader().getResourceAsStream("application.properties");

chances are you get this:

bad port configuration: ${project.host.db3.database.port}
java.lang.NumberFormatException: For input string: "${project.host.db3.database.port}"

In answer to yorkw, now i can have the same war to be deployed in several environments and configure the host with -Denvironment=development, so i can deploy a properties file for development, production, etc. and simply use:

<bean id="systemPropertiesLoader"   
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" value="#{@systemProperties}" />
        <property name="targetMethod" value="putAll" />
        <property name="arguments">
    <util:properties location="classpath:**${environment}/**environment.properties" />
</property>
</bean>

Otherwise i should have the application.properties substituted before deployment for every environment. I'm sure there are better solutions than this.

user311174
  • 1,738
  • 1
  • 18
  • 17