Perhaps you may be best to externalise your environment-specific config to a properties file and then use property substitution in your applicationContext.xml
. That way you can easily access the properties programatically and keep environment-specific config separate from the main Spring config.
mongo.properties
mongo.host=127.0.0.1
mongo.port=27017
mongo.dbname=myDb
applicationContext.xml
<context:property-placeholder location="classpath:/mongo.properties"/>
<!-- Directs Java to correct Mongo DB address and port -->
<mongo:mongo host="${mongo.host}" port="${mongo.port}" />
<mongo:db-factory dbname="${mongo.dbname}" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
Then in your code:
Resource resource = new ClassPathResource("/mongo.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
String mongoPort = props.getProperty("mongo.port");