无法解析上下文的占位符:property-placeholder

时间:2013-02-22 10:05:45

标签: 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"?>
<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)

2 个答案:

答案 0 :(得分:1)

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

改变自,

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

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

答案 1 :(得分:1)

在我重新启动计算机之前,似乎系统变量无效。之后它运行良好,(一旦我修复了monog.dbName的错字)。

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

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