I have a Spring 4 MVC app in which I'd like to pass in the environment (profile) from the command line and have it read the correct environment-specific .properties
files on startup.
These properties files essentially contain different jdbc
connection strings so each environment can connect to the correct database.
I'm still learning Spring and Java in general, so having a hard time figuring this out.
In web.xml
I define 3 environments/profiles
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>test, dev, prod</param-value>
</context-param>
I have 3 environment specific files under resources directory. My understanding is that Spring will try to source the appropriate environment specific file and a generic application.properties
file (if one exists, and it doesn't here) to fall back on.
> \ls src/main/webapp/resources/properties/
application-dev.properties application-prod.properties application-test.properties
Each file is pretty simple, just jdbc connection params for that environment. Example:
jdbc.driverClassName=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/galapagos
jdbc.username=foo
jdbc.password=
Lastly in my servlet file spring-web-servlet.xml
, I read the application properties file and use it to establish a connection
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
....
<!-- Database / JDBC -->
<beans:bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location" value="resources/properties/application.properties" />
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<beans:property name="driverClassName" value="${jdbc.driverClassName}" />
<beans:property name="url" value="${jdbc.url}" />
<beans:property name="username" value="${jdbc.username}" />
<beans:property name="password" value="${jdbc.password}" />
</beans:bean>
....
</beans:beans>
This is erroring out, because it's trying to look for a resources/properties/application.properties
which doesn't exist. But I'm not sure what else to put in there. How do I get it to dynamically read the correct environment's file on startup?
I saw some examples like this one using context listeners, but honestly I'm still learning Spring MVC and don't really understand what those are trying to do
Thanks!