0

I am trying to understand from where does the value of a variable comes from, when it is referred in Spring xml file.

For example:

<context:property-placeholder location="classpath:/${com.example.deploy.environment}/com.example.config/mysql.properties" ignore-resource-not-found="false" />

Where is the value of com.example.deploy.environment defined? In my project I searched all over, however i couldn't find anywhere where this values is defined.

Any information in understanding this would be of great help.

CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • 1
    Q: In a spring.xml file, where does the value of a variable comes from? A: It's important to recognize that it can be several different *kinds* of "variable" (including - but not limited to, simple Java "System.Property" values), and can be defined - or overriden - in several different places. Look at the links Sanjay Rawat cited. Be sure to "upvote" and "accept" his answer if you found it helpful. Also: what about [this](http://stackoverflow.com/questions/35933597) question you asked a few days ago? – paulsm4 Mar 19 '16 at 07:01
  • 2
    PS: If you're running your Spring app from an IDE (like Eclipse), you'd probably add `-Dcom.example.deploy.environment=abcxyz` to your Arguments > VM arguments tab. Similarly, if you were running on TomCat or an App server like WebSphere, you'd also probably define your arguments with a "-D" in the JVM settings. – paulsm4 Mar 19 '16 at 07:10

1 Answers1

2

This value can come from a variety of source:

  1. application.properties file which you can define in PropertyPlaceholderConfigurer bean.

    <bean id="mailProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:application.properties" />
    </bean>
    
    ...
    //Inside application.properties
    com.example.deploy.environment=prod
    
  2. You can provide via command-line:

    With Maven vm arguments for JVM System property:

    mvn package -Dcom.example.deploy.environment=prod
    

    Running Spring Boot Application:

    java -jar app.jar --com.example.deploy.environment="prod"
    
    1. From System Environment variable of the Operating System. You might have to restart after setting environment variable. See below for windows:

In Windows

Refer this doc and this article for more info.

Sanjay Rawat
  • 2,304
  • 15
  • 30