2012-09-12 73 views
7

的环境变量,我需要从我applicationContext.xml阅读从applicationContext.xml中

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location" value="${PATH_ENV}/myprop.properties" /> 
</bean> 

我怎样才能做到这一点看在我的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> 

定义的环境变量?


最后,我已经做了下:

1 context.xml中定义的环境变量:

<Environment name="PATH_ENV" type="java.lang.String"/> 

2在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>/WEB-INF/</env-entry-value> 
    </env-entry> 

ENV进入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-入门价值为什么?

+0

如果有人遇到这个问题,它应该是'文件:C:/ V3/' – Jamie

回答

0

您可以使用context-param,这将工作。

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

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

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

(要使用JEE命名空间,则必须declare它)。

它定义了一个名为“PATH_ENV”的spring 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项目进行测试)

+0

我无法阅读上下文am.Is这是读取applicationContext.xml中的上下文参数的正确方法吗? value =“$ {PATH_ENV} /myprop.properties” – Geme

+0

上下文参数不同于环境条目。你的问题说环境条目(''),那么你为什么现在谈论上下文参数('')?至于如何引用它,我会编辑以包含样本。 – meriton

+0

org.springframework.beans.factory.BeanCreationException:创建名为'PATH_ENV'的bean时出错:init方法的调用失败;嵌套异常是javax.naming.NameNotFoundException:注意:PATH_ENV没有特定的权限 – Geme

0

为什么不直接使用下面的?

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

属性文件需要有一个基于相同变量的相对路径 – Geme

+0

为什么不把.properties文件放在资源中或项目中类路径中的类似文件夹中? –

-1

我解决了一些东西,我想,类似的。

我创建的路径改变部分Windows系统变量:

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

然后用这个,我完成春季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> 

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

+0

我的问题是变量不能是一个系统变量 – Geme