Dev Zhou
Dev Zhou

Reputation: 905

How spring load configuration file by environment?

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.

Upvotes: 0

Views: 799

Answers (2)

Sainik Kumar Singhal
Sainik Kumar Singhal

Reputation: 801

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>

Upvotes: 1

Jiri Tousek
Jiri Tousek

Reputation: 12440

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.

Upvotes: 1

Related Questions