从applicationContext.xml中读取环境变量

时间:2022-12-16 17:21:12

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

<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中定义完整路径为什么?

4 个解决方案

#1


4  

You can lookup JNDI entries (both environment entries and resources) with the JndiObjectFactoryBean or <jee:jndi-lookup>:

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

<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. You can now inject it into other beans:

它定义了一个名为“PATH_ENV”的spring bean,它包含(作为字符串)在环境条目中配置的路径。您现在可以将其注入其他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项目来测试它)

#2


0  

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>

#3


0  

Why not just use the following?

为什么不使用以下?

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

#4


-1  

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

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

#1


4  

You can lookup JNDI entries (both environment entries and resources) with the JndiObjectFactoryBean or <jee:jndi-lookup>:

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

<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. You can now inject it into other beans:

它定义了一个名为“PATH_ENV”的spring bean,它包含(作为字符串)在环境条目中配置的路径。您现在可以将其注入其他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项目来测试它)

#2


0  

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>

#3


0  

Why not just use the following?

为什么不使用以下?

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

#4


-1  

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

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