从applicationContext.xml中读取环境变量 [英] Read an environment variable from applicationContext.xml

查看:623
本文介绍了从applicationContext.xml中读取环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读我的web.xml中定义的环境变量

I need read an environment variable defined in my 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中定义环境变量:

1 Define environment variable in context.xml:

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

2在web.xml中定义env-entry

2 Define env-entry in 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在applicationContext.xml中定义

3 Define in 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>

我有下一个问题:

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

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

I cant define a full path in env-entry-value Why?

推荐答案

你可以使用JndiObjectFactoryBean或 < jee:jndi-lookup>

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

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

(要使用jee-namespace,你必须声明它)。

(To use the jee-namespace, you must declare it).

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

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:

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

剩余的难度是连接字符串。 (不幸的是,没有JndiPlaceholderConfigurer会用JNDI环境条目替换占位符,所以你不能使用 $ {property} / foo 语法来连接,并且必须提供另一个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:

<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项目来测试它)

(code untested as I don't have a Spring project at hand to test it)

这篇关于从applicationContext.xml中读取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆