简体   繁体   English

从applicationContext.xml读取环境变量

[英]Read an environment variable from applicationContext.xml

I need read an environment variable defined in my web.xml 我需要阅读在我的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>

from my applicationContext.xml 从我的applicationContext.xml

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

How can I do this ? 我怎样才能做到这一点 ?


Finally I have done the next: 最后,我完成了下一个:

1 Define environment variable in context.xml: 1在context.xml中定义环境变量:

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

2 Define env-entry in web.xml 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>

3 Define in applicationContext.xml 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>

This is run correctly, However if I define a full path in: 这可以正常运行,但是如果我在以下路径中定义了完整路径:

<env-entry-value>C:/V3/</env-entry-value>

I have the next problem: 我有下一个问题:

java.io.FileNotFoundException: Could not open ServletContext resource [/C:/V3/aesantasa.properties]

I cant define a full path in env-entry-value Why? 我无法在env-entry-value中定义完整路径,为什么?

You can lookup JNDI entries (both environment entries and resources) with the JndiObjectFactoryBean or <jee:jndi-lookup> : 您可以使用JndiObjectFactoryBean或<jee:jndi-lookup>查找JNDI条目(环境条目和资源):

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

(To use the jee-namespace, you must declare it). (要使用jee-namespace,必须声明它)。

That defines a spring bean named "PATH_ENV" that contains (as a string) the path configured int the environment entry. 这定义了一个名为“ PATH_ENV”的spring bean,其中包含(作为字符串)在环境条目中配置的路径。 You can now inject it into other beans: 您现在可以将其注入其他bean:

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

The remaining difficulty is concatenating the strings. 剩下的困难是串联字符串。 (Unfortunately, there is no JndiPlaceholderConfigurer that would replace placeholders with JNDI environment entries, so you can't use the ${property}/foo syntax to concatenate, and must supply yet another bean definition: (不幸的是,没有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>

(code untested as I don't have a Spring project at hand to test it) (代码未经测试,因为我手头没有Spring项目来对其进行测试)

You can use context-param , that will work. 您可以使用context-param ,它将起作用。

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

Why not just use the following? 为什么不只使用以下内容?

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

I solved something, I think, similar. 我认为我解决了类似的问题。

I create a Windows System Variable with the changing part of the path: 我使用路径的更改部分创建一个Windows系统变量:

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

And then with this I complete the path on Spring AppContext like this: 然后,我像这样完成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>

I don't know if really help, but for me works 我不知道是否真的有帮助,但对我而言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM