6

In a Spring file I have:

   <bean id="propertyConfigurer" class="org.myapp.MyConfigurator">
        <property name="locations">
        <list>
                       <value>classpath:configuration-${env}.properties</value>
        </list>
    </property>
</bean>

the ${env} variable is defined in maven's profile. But when I run from eclipse the application in tomcat 6 (published) it doesn't look in maven. So how can I set the variable for Tomcat?

Thanks

Randomize

Tarlog
  • 10,024
  • 2
  • 43
  • 67
Randomize
  • 8,651
  • 18
  • 78
  • 133
  • http://stackoverflow.com/questions/3965446/how-to-read-system-environment-variable-in-spring-applicationcontext – Dave Newton Oct 26 '11 at 14:45

3 Answers3

6

Add system variable in Eclipse: Go to Run --> Run Configurations --> Tomcat Select Arguments tab and add to VM arguments -Denv=blabla

Tarlog
  • 10,024
  • 2
  • 43
  • 67
5

To define a variable in in the tomcat context.xml that can be used in spring add this line to the right context in context.xml.

<Parameter name="env" value="ABCDEFG"  override="false"/>
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • override: Set this to false if you do not want a for the same parameter name, found in the web application deployment descriptor, to override the value specified here. By default, overrides are allowed. – Matias Sebastiao Jul 17 '17 at 19:28
2

Tarlog solution can be solve your problem but only inside eclipse, if you remove your server definition from eclipse you lost the definition and you need to add it again each time you change your IDE or delete the server definition.

So better way create a shell or bat file(like runServer.sh/.bat) according to your environment and add this parameter to the JAVA_OPTS variable so this variable called when catalina.sh/.bat is running (startup.sh/.bat called catalina script inside it). You can use this approach at your local, test and prod environment.

Windows: runServer.bat

set JAVA_OPTS="-Dvariable=value"
tomcat/bin/startup.bat

Linux: runServer.sh

export JAVA_OPTS="-Dvariable=value"
tomcat/bin/startup.sh
erhun
  • 3,549
  • 2
  • 35
  • 44