从applicationContext.xml读取环境变量

8
我需要读取在我的web.xml中定义的环境变量。
<env-entry>
    <description>Path Repositorio NFS</description>
    <env-entry-name>PATH_ENV</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>C:/V3</env-entry-value>
</env-entry>

从我的 applicationContext.xml 文件中。
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="${PATH_ENV}/myprop.properties" />
</bean>

我该如何做这个?
最终我完成了以下事项:
1. 在context.xml中定义环境变量。
<Environment name="PATH_ENV" type="java.lang.String"/>

2 在 web.xml 中定义 env-entry
<env-entry>
    <description>Path Repositorio NFS</description>
    <env-entry-name>PATH_ENV</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>/WEB-INF/</env-entry-value>
  </env-entry>

3 在applicationContext.xml文件中定义。
<bean id="configurationPath" class="org.springframework.jndi.JndiObjectFactoryBean">  
    <property name="jndiName">  
        <value>java:comp/env/PATH_ENV</value>  
    </property>  
</bean>  

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="location">
            <bean factory-bean="configurationPath" factory-method="concat">
                <constructor-arg value="myprop.properties"/>
            </bean>
        </property>
    </bean>

这个可以正常运行,但是如果我在中定义了完整路径:
<env-entry-value>C:/V3/</env-entry-value>

我有下一个问题:
java.io.FileNotFoundException: Could not open ServletContext resource [/C:/V3/aesantasa.properties]

我无法在env-entry-value中定义完整路径,为什么?

如果有人看到这个问题,它应该是 <env-entry-value>file:C:/V3/</env-entry-value> - Jamie
4个回答

4

您可以使用JndiObjectFactoryBean或<jee:jndi-lookup>查找JNDI条目(包括环境条目和资源):

<jee:jndi-lookup id="PATH_ENV" jndi-name="PATH_ENV"/>

要使用jee-namespace,您必须声明它。

这定义了一个名为“PATH_ENV”的spring bean,其中包含(作为字符串)在环境条目中配置的路径。现在,您可以将其注入到其他bean中:

<bean class="xy.Foo">
    <property name="path" ref="PATH_ENV"/>
</bean>

剩下的难点在于连接字符串。(不幸的是,没有JndiPlaceholderConfigurer可以将占位符替换为JNDI环境条目,因此您不能使用${property}/foo语法来连接字符串,必须提供另一个bean定义:)
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <bean factory-bean="PATH_ENV" factory-method="concat">
            <constructor-arg>/myprop.properties</constructor-arg>
        </bean>
    </property>
</bean>

(代码未经测试,因为我手头没有Spring项目来测试它)


我无法读取context-param。这是在applicationContext.xml中读取context-param的正确方式吗?value="${PATH_ENV}/myprop.properties" - Geme
一个上下文参数不同于环境条目。你的问题中提到了环境条目(<env-entry>),那么为什么现在又谈论上下文参数(<context-param>)呢?至于如何引用它,我会编辑并包含一个示例。 - meriton
org.springframework.beans.factory.BeanCreationException: 创建名为'PATH_ENV'的bean时出错:初始化方法调用失败;嵌套异常是javax.naming.NameNotFoundException:名称PATH_ENV在此上下文中未关联。 - Geme
这个解决方案对我来说是最正确的,但需要确保 PATH_ENV 以 "file:" 开头。 - Jamie

0
你可以使用 context-param,它会起作用。
<context-param>
    <param-name>PATH_ENV</param-name>
    <param-value>C:/V3</param-value>
</context-param>

0
为什么不直接使用以下代码?
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="file:C:/V3/myprop.properties"/>
</bean>

属性文件需要基于相同的变量有一个相对路径。 - Geme
为什么不把.properties文件放在项目中的资源或类路径下的类似文件夹中呢? - Oleksandr Bondarenko

-1

我解决了类似的问题。

我创建了一个带有路径变化部分的Windows系统变量:

my computer --> advanced options --> environment options --> Systeme Variable

然后,我可以像这样完成Spring AppContext上的路径:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

   <property name="locations">  
       <list>
              <value>file:${PARENT_PATH}/conf/dev/jdbc.properties</value>
       <list>           
   </property>
</bean>

我不知道这是否真的有帮助,但对我来说有效


我的问题是变量不能是系统变量。 - Geme

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接