0

I use spring framework in my application,and i use some configuration files,but in different environment,i need to use different properties like db config properties.Now i put different files in different path,then i use maven profile method to package different WAR.

Now,i want to package only one WAR in all environment,and want by transfer different parameters to use different configuration files.In addition i don't want to put the configuration file out of the project.

Dev Zhou
  • 865
  • 2
  • 14
  • 21
  • What exactly is the result you want? A single WAR being built that contains configuration files for every supported environment? A separate WAR being built for every environment, each containing only the appropriate configuration? A single WAR being built with a single-environment configuration, based on a build parameter? – Jiri Tousek Oct 17 '15 at 13:56

2 Answers2

1

You can use spring bean profiles or conditional beans.You can use following configuration to define propertyplaceholder beans for each environment.

        <beans profile="environment1">
<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="location">
        <value>database_env1.properties</value>
    </property>
</bean>
</beans>

<beans profile="environment2">
<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="location">
        <value>database_env2.properties</value>
    </property>
</bean>
</beans>
1

For database configuration, a possible approach is to define the JDBC DataSource as a JNDI resource directly in your application server that you then use (see how) - this way your WAR file doesn't contain the connection information, the runtime environment does hold this info instead.

One advantage of this approach is that the connection information then can be managed by a server administrator instead of the application developer.

Not sure it meets your "no configuration outside project" requirement though.

Community
  • 1
  • 1
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43