小编Gem*_*eme的帖子

从applicationContext.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>
Run Code Online (Sandbox Code Playgroud)

从我的 applicationContext.xml

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="${PATH_ENV}/myprop.properties" />
</bean>
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点 ?


最后我做了下一个:

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

<Environment name="PATH_ENV" type="java.lang.String"/>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

这是正确运行的,但是如果我在以下位置定义完整路径:

<env-entry-value>C:/V3/</env-entry-value>
Run Code Online (Sandbox Code Playgroud)

我有下一个问题:

java.io.FileNotFoundException: Could not open ServletContext resource [/C:/V3/aesantasa.properties]
Run Code Online (Sandbox Code Playgroud)

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

java xml spring applicationcontext

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

applicationcontext ×1

java ×1

spring ×1

xml ×1