简体   繁体   English

Tomcat + Spring和环境变量

[英]Tomcat + Spring and environment variables

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. $ {env}变量在maven的配置文件中定义。 But when I run from eclipse the application in tomcat 6 (published) it doesn't look in maven. 但是当我从eclipse中运行tomcat 6(已发布)中的应用程序时,它看起来并不是maven。 So how can I set the variable for Tomcat? 那么如何为Tomcat设置变量呢?

Thanks 谢谢

Randomize 随机化

在Eclipse中添加系统变量:转到Run - > Run Configurations - > Tomcat Select Arguments选项卡并添加到VM参数-Denv=blabla

要在tomcat context.xml中定义可在spring中使用的变量,请将此行添加到context.xml的右侧上下文中。

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

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). 因此,根据您的环境创建shell或bat文件(如runServer.sh/.bat)更好的方法,并将此参数添加到JAVA_OPTS变量,以便在运行catalina.sh/.bat时调用此变量(startup.sh/.bat里面叫catalina脚本)。 You can use this approach at your local, test and prod environment. 您可以在本地,测试和产品环境中使用此方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM