eclipse Tomcat + Spring 和环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7904112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 17:14:56  来源:igfitidea点击:

Tomcat + Spring and environment variables

javaeclipsespringtomcatmaven

提问by Randomize

In a Spring file I have:

在 Spring 文件中,我有:

   <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?

${env} 变量在 maven 的配置文件中定义。但是,当我从 Eclipse 运行 tomcat 6(已发布)中的应用程序时,它在 maven 中看不到。那么如何为Tomcat设置变量呢?

Thanks

谢谢

Randomize

随机化

采纳答案by Tarlog

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

在 Eclipse 中添加系统变量:转到运行 --> 运行配置 --> Tomcat 选择参数选项卡并添加到 VM 参数 -Denv=blabla

回答by Ralph

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

要在 tomcatcontext.xml中定义一个可以在 spring 中使用的变量,请将这一行添加到 context.xml.

<Parameter name="env" value="ABCDEFG"  override="false"/>

回答by erhun

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.

Tarlog 解决方案可以解决您的问题,但只能在 eclipse 中解决,如果您从 eclipse 中删除服务器定义,您将丢失该定义,并且每次更改 IDE 或删除服务器定义时都需要重新添加它。

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.

所以更好的方法是根据您的环境创建一个 shell 或 bat 文件(如 runServer.sh/.bat)并将此参数添加到 JAVA_OPTS 变量中,以便在 catalina.sh/.bat 运行时调用此变量(startup.sh/.bat在其中称为 catalina 脚本)。您可以在本地、测试和生产环境中使用这种方法。

Windows: runServer.bat

Windows:runServer.bat

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

Linux: runServer.sh

Linux:runServer.sh

export JAVA_OPTS="-Dvariable=value"
tomcat/bin/startup.sh