0

I'm pretty new to Java and there's a project I'm working on that I'm struggling with.

For the longest time we've had a really clunky build process for this app, where you have to manually go in and change values in the pom.xml file to switch from test to prod values. I'm trying to fix that so you can instead build using a profile flag, but I'm running into some trouble with one piece. There's an applicationContext.xml file that has the database URL hardcoded into it, and I want to be able to change that based on the profile, but I'm having trouble figuring out how to do that.

The line in question looks like this:

<property name="url" value="jdbc:mysql://databaseserver:3306/testDatabase" />

How can I have that load the property I'm setting up in my application.properties file?

app.database.url="jdbc:mysql://databaseserver:3306/testDatabase"
Whitewind617
  • 357
  • 1
  • 2
  • 10

2 Answers2

0

You could do it in few ways. For instance, you could either place your properties directly inside the pom.xml file within a profile, or place them inside a .properties file and decide which file you should use on a profile level.

Here is the first way of doing this: Firstly, you would write something like that in your main pom.xml :

     <project> 

     ...

      <profiles>
        <profile>
          <id>development</id>
          <activation>
            <property>
              <name>env</name>
              <value>dev</value>
            </property>
          </activation>
          <properties>
          <app.database.url>jdbc:mysql://databaseserver:3306/testDatabase</app.database.url>
          </properties>
        </profile>  
<profile>
          <id>production</id>
          <activation>
            <property>
              <name>env</name>
              <value>prd</value>
            </property>
          </activation>
          <properties>
          <app.database.url>jdbc:mysql://anotherserver:1234/veryImportantProductionDatabase</app.database.url>
          </properties>
        </profile>
        ...   </profiles> 
</project>

Then you would replace your property tag with a given placeholder, that is

 <property name="url" value="${app.database.url}" />

and voilà. Hope that helps

JacekDuszenko
  • 136
  • 2
  • 11
  • That's exactly what I've done though, and still the application.Context file will not be able to use that property, it will just stay as the placeholder. – Whitewind617 Aug 03 '18 at 13:31
0

Simple just reference the properties file in your applicationContext.xml

<property name="url" value="${app.database.url} />

JpersaudCodezit
  • 143
  • 2
  • 13