【发布时间】:2012-05-03 11:05:16
【问题描述】:
我有一个 Spring 3.1 应用程序,我尝试在上下文文件中使用系统变量。变量“JAVA_MY_ENV”是在我的系统上定义的(在 Windows 上,它位于控制面板的“系统变量”中)。
在 web.xml 中,我可以将它用作变量并且它可以工作,它成功地被变量的实际值替换(比如说“electrotype”):
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log/${JAVA_MY_ENV}.log4j.properties</param-value>
</context-param>
我也可以在我的主要“bean”上下文中使用它来进行导入,它也可以工作:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- (...) -->
<import resource="classpath:spring/app-config.xml" />
<import resource="classpath:spring/env/context-env-${JAVA_MY_ENV}.xml" />
</beans>
但是在“app-config.xml”,我的其他上下文文件之一,我尝试了这个,它不起作用:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id="appConfiguration" class="com.xxx.app.AppConfiguration">
<constructor-arg value="${JAVA_MY_ENV}" />
</bean>
</beans>
com.xxx.app.AppConfiguration 接收字符串“${JAVA_MY_ENV}”作为构造函数参数,而不是它的解释值!
我不确定在哪里解释 ${} 变量以及在哪里不解释。
有没有办法可以将解释的 ${JAVA_MY_ENV} 值传递给我的 com.xxx.app.AppConfiguration 构造函数?
【问题讨论】:
标签: java spring spring-mvc servlets