从applicationContext.xml中读取环境变量

时间:2012-09-12 15:20:31

标签: java xml spring applicationcontext

我需要阅读我的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中定义完整路径为什么?

4 个答案:

答案 0 :(得分:4)

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

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

(要使用jee-namespace,您必须declare)。

它定义了一个名为“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项目来测试它)

答案 1 :(得分:0)

您可以使用context-param,这将有效。

<context-param>
    <param-name>PATH_ENV</param-name>
    <param-value>C:/V3</param-value>
</context-param>

答案 2 :(得分:0)

为什么不使用以下内容?

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="file:C:/V3/myprop.properties"/>
</bean>

答案 3 :(得分:-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>

我不知道是否真的有帮助,但对我而言

相关问题