gpt4 book ai didi

spring - 使用Spring表达式语言解析系统环境变量

转载 作者:行者123 更新时间:2023-12-02 22:34:02 25 4
gpt4 key购买 nike

我想在 spring servlet 配置文件中使用“Spring 表达式语言”解析系统环境变量。我的第一个方法是:

 <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.#{systemEnvironment.THREAD_ENV}.properties" />

这引发了以下异常:

Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 18): Field or property 'THREAD_ENV' cannot be found on object of type 'java.util.Collections$UnmodifiableMap'

然后我尝试过:

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.#{systemEnvironment['THREAD_ENV']}.properties" />

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.#{systemEnvironment[THREAD_ENV]}.properties" />

都失败并解析为空字符串。我正在使用 Tomcat6,并且在重新启动 Tomcat 服务器之前导出此变量:

export THREAD_ENV=live;
/etc/init.d/tomcat6 restart;

想提一下,所有三种方法都适用于我的一些 Tomcat6 实例,但不适用于所有实例 - 出现这种奇怪行为的原因可能是什么?知道我做错了什么吗?

最佳答案

SpEL 变量是 systemProperties,而不是 systemEnvironment

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.#{systemProperties['THREAD_ENV']}.properties" />

关于spring - 使用Spring表达式语言解析系统环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23899982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com