[color=red]1. [b]System.getProperty()获取tomcat自定义变量[/b][/color]
[url]http://zwxiaole.iteye.com/blog/1832053[/url]
环境 tomcat 集群 有一个定时器
为了防止所有集群的服务器都跑定时而出错
选择其中一台服务器 修改tomcat bin 目录下的
windows catalina.bat
linux catalina.sh
你定义的变量 必须以-D开头 否则System.getProperty()无法获取到该参数值
以 scheduler.start为例子
set JAVA_OPTS= -Dscheduler.start=true
System.getProperty('scheduler.start')可以获取到 true值
当获取到true时 那台服务器就会开始定时任务 其他服务器就不会开启
[color=red][b]2. catalina.bat或者eclipse服务器里面设定的环境变量[/b]
[url]http://stackoverflow.com/questions/686591/is-there-a-way-to-resolve-system-properties-in-a-web-xml-file[/url][/color]
[url]http://stackoverflow.com/questions/3965446/how-to-read-system-environment-variable-in-spring-applicationcontext[/url]
A:在catalina.bat增加JAVA_OPTS="...... -DPANDY_HOME=e:/aaa ...... "
B:在eclipse的tomcat服务器里面设定环境变量(不确定)
-DPANDY_HOME=e:/aaa
在applicationcontext.xml引用
<bean id="appProperties" class="com.pandy.config.AppProperties">
<property name="locations">
<list>
<value>classpath:resources/default.properties</value>
<value>file:${PANDY_HOME}/conf/jdbc.properties</value>
</list>
</property>
</bean>
[color=red][b]3.Spring类里面读取变量[/b][/color]
[url]http://stackoverflow.com/questions/10324702/propertyplaceholderconfigurer-and-environment-variables-in-properties-files[/url]
A:
E.g.
@Value("#{ systemProperties['JAVA_MY_ENV'] }")
private String myVar;
or
<property name ="myVar" value="#{systemProperties['JAVA_MY_ENV']}"/>
B:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:someprops.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true" />
<property name="searchSystemEnvironment" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
You must also remember to pass the parameter into the program using
[color=blue] -DJAVA_MY_ENV=xyz[/color]
This way when you run the production version you can pass one thing and when you are running tests another.
Also what I often what I do is something like this:
<property name="locations">
<list>
<value>classpath:someprops.properties</value>
<value>classpath:someprops-{environment}.properties</value>
</list>
</property>
where environment is prod/stage/test/int/ci/local (1 per environment - you may only have 2 or 3 for now). You can pass the environment variable to the program. Any properties which should be the same regardless of if its production/running on your local pc/tests would be in the someprops.properties property file. Any ones specific to the environment/way its being run as will go in the more specific file (you should put it in the someprops.properties file as well as a default unless overridden mechanism)
E.g. in classpath:someprops.properties
url=www.mysite.com
in classpath:someprops-local.properties
url=localhost
By using this basic idea you can separate tests and the program's normal running properties in a clean manner.