java 无法解析上下文的占位符:property-placeholder
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15021535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not resolve placeholder for context:property-placeholder
提问by Ramesh
I am trying to set my app to use different data source definitions, based on an environment variable, but I'm running into the below error. I looked at many similar questions, but they don't seem to be the same.
我正在尝试根据环境变量将我的应用程序设置为使用不同的数据源定义,但我遇到了以下错误。我看了很多类似的问题,但它们似乎并不相同。
I have set up multiple properties files:
我已经设置了多个属性文件:
env-dev.properties
env-test.properties
env-prod.properties
env-.properties
I have created a system property called MEM_ENV with the value "dev"
我创建了一个名为 MEM_ENV 的系统属性,其值为“dev”
My spring xml file looks like this:
我的 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>
My properties files look like this:
我的属性文件如下所示:
mongo.server=aServer.com
mongo.port=10003
monog.dbName=aDBName
mongo.userName=aUserName
mongo.password=aPassword
The exception I'm getting is:
我得到的例外是:
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)
回答by Jayamohan
You have used the classpath
resolver wrongly. Remove one unnecessay *
.
您classpath
错误地使用了解析器。去掉一个不必要的*
。
Change from,
改变从,
<context:property-placeholder location="classpath*:*env-${MEM_ENV}.properties" />
to
到
<context:property-placeholder location="classpath*:env-${MEM_ENV}.properties" />
回答by Ramesh
It seems that the system variable didn't work until I rebooted my machine. After that it worked fine, (once I fixed my typo of monog.dbName).
在我重新启动机器之前,系统变量似乎不起作用。之后它工作正常(一旦我修复了monog.dbName的错字)。
I'm not sure if it was the reboot of Windows, or the restart of Eclipse, or restart of Tomcat that fixed the problem, as all of those occurred with my reboot.
我不确定是否是 Windows 的重新启动、Eclipse 的重新启动或 Tomcat 的重新启动解决了问题,因为所有这些都是在我重新启动时发生的。
Hope this helps someone else - if you have this problem, first try a reboot.
希望这对其他人有所帮助 - 如果您遇到此问题,请先尝试重新启动。