Java 无法解析上下文的占位符:属性占位符

Java 无法解析上下文的占位符:属性占位符,java,database,spring,properties,environment-variables,Java,Database,Spring,Properties,Environment Variables,我试图根据环境变量将我的应用程序设置为使用不同的数据源定义,但遇到以下错误。我看了很多类似的问题,但它们似乎不一样 我已设置多个属性文件: env-dev.properties env-test.properties env-prod.properties env-.properties 我创建了一个名为MEM_ENV的系统属性,其值为“dev” 我的spring xml文件如下所示: <?xml version="1.0" encoding="UTF-8"?> <b

我试图根据环境变量将我的应用程序设置为使用不同的数据源定义,但遇到以下错误。我看了很多类似的问题,但它们似乎不一样

我已设置多个属性文件:

env-dev.properties
env-test.properties
env-prod.properties
env-.properties
我创建了一个名为MEM_ENV的系统属性,其值为“dev”

我的spring xml文件如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:property-placeholder
        location="classpath*:*env-${MEM_ENV}.properties" />
    <bean id="mongoDataSource" class="com.iLearn.persistence.base.MongoDataSourceImpl">
        <property name="server" value="${mongo.server}" />
        <property name="port" value="${mongo.port}" />
        <property name="dbName" value="${mongo.dbName}" />
        <property name="userName" value="${mongo.userName}" />
        <property name="password" value="${mongo.password}" />
    </bean>
mongo.server=aServer.com
mongo.port=10003
monog.dbName=aDBName
mongo.userName=aUserName
mongo.password=aPassword
我得到的例外是:

    message org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'mongoDataSource' defined in class path resource [springAppConfig.xml]: Could not resolve placeholder 'mongo.server' in string value "${mongo.server}"

description The server encountered an internal error (org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'mongoDataSource' defined in class path resource [springAppConfig.xml]: Could not resolve placeholder 'mongo.server' in string value "${mongo.server}") that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'mongoDataSource' defined in class path resource [springAppConfig.xml]: Could not resolve placeholder 'mongo.server' in string value "${mongo.server}"
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
    org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:357)
    com.iLearn.security.AccessFilter.doFilter(AccessFilter.java:43)

您错误地使用了
类路径
解析器。删除一个不必要的
*

从,

<context:property-placeholder location="classpath*:*env-${MEM_ENV}.properties" />


在我重新启动机器之前,系统变量似乎不起作用。在那之后,它工作得很好(一旦我修复了monog.dbName的输入错误)

我不确定是Windows的重新启动,还是Eclipse的重新启动,还是Tomcat的重新启动解决了这个问题,因为所有这些都是在我重新启动时发生的


希望这对其他人有所帮助-如果您遇到此问题,请先尝试重新启动。

顺便说一句,我使用的是Spring版本3.2.1.ReleaseTry location=“classpath:/env-${MEM_env}.properties”/>它不起作用-它会创建一个不同的异常-FileNotFoundOk这意味着前面的文件已解决,因此,您只需确定MEM_ENV系统变量的正确值,以及ENV-dev.properties文件中定义了mongo.server属性,我在这里看不到其他情况。我也这么想,但我似乎看不到问题所在,所以我想我会看看我是否遗漏了一些更微妙的东西。这似乎没有什么区别。不管是一个还是两个,我仍然得到上面的例外。如果我一起删除“classpath*:”,我会得到一个不同的异常,即它找不到属性文件。这让我想到,在这种情况下,它是在查找属性文件。。。
<context:property-placeholder location="classpath*:env-${MEM_ENV}.properties" />