0

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!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2490003
  • 10,706
  • 17
  • 79
  • 155

2 Answers2

0

By default, spring will to try to find resources/properties/application.properties to load properties. This file is auto-detected. This is your problem and you have to provide one. If you don't like to have an application.properties file, you can override it names by specifing spring.config.name environment property and its location by using spring.config.location environment property.

In your web.xml,

<context-param>
 <param-name>spring.profiles.active</param-name>
 <param-value>test, dev, prod</param-value>
</context-param>

You are activating 3 profiles at the same time. I would suggest to define which profile is activated in your application.properties. For example :

spring.profiles.active=dev

Then, the specific environnment file will be loaded and will take precedence over the default property file.

victor gallet
  • 1,819
  • 17
  • 25
  • 5
    **Spring** won't look for application.properties by default, it is **Spring Boot** which will do so, please don't mislead others – Dmytro Grynets Apr 27 '17 at 12:48
0

You may pass through the command prompt while building. eg: mvn clean package -Pdev

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 22 '22 at 12:27