无法解析 Spring 属性占位符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4779572/
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 Spring property placeholder
提问by mjgirl
I have my config:
我有我的配置:
<context:property-placeholder location="classpath:idm.properties" />
<bean id="idmPropertyHolder" class="fi.utu.resurssitilaus.idm.IdmPropertyHolder">
<property name="url" value="${idm.url}" />
<property name="user" value="${idm.user}" />
<property name="password" value="${idm.password}" />
<property name="proxyHost" value="${http.proxyHost}" />
<property name="proxyPort" value="${http.proxyPort}" />
</bean>
I get the error
我收到错误
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'idmPropertyHolder' defined in ServletContext resource [/WEB-INF/idm-config.xml]: Could not resolve placeholder 'idm.url'
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:272)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:640)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:615)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:405)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3934)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4429)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:526)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:630)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:514)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1288)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:297)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:761)
at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1473)
at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:824)
at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:350)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:196)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
24.1.2011 9:19:05 org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
24.1.2011 9:19:05 org.apache.catalina.core.ApplicationContext log
INFO: Shutting down log4j
I know this could be missing properties file, but I have it in my classpath just right. What is missing?
我知道这可能缺少属性文件,但我的类路径中正好有它。有什么不见了?
My web.xml:
我的 web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/base-config.xml
/WEB-INF/idm-config.xml
/WEB-INF/ldap-config.xml
/WEB-INF/sec-config.xml
</param-value>
</context-param>
<!-- Reads request input using UTF-8 encoding -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<!-- Handles all requests into the application -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/app-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>tiles</servlet-name>
<servlet-class>org.apache.tiles.web.startup.TilesServlet</servlet-class>
<init-param>
<param-name>
org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
</param-name>
<param-value>
/WEB-INF/tiles-config.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
回答by jabley
You may have more than one org.springframework.beans.factory.config.PropertyPlaceholderConfigurerin your application. Try setting a breakpoint on the setLocationsmethod of the superclass and see if it's called more than once at application startup. If there is more than one org.springframework.beans.factory.config.PropertyPlaceholderConfigurer, you might need to look at configuring the ignoreUnresolvablePlaceholdersproperty so that your application will start up cleanly.
您的申请中可能有多个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。尝试setLocations在超类的方法上设置断点,看看它是否在应用程序启动时被多次调用。如果有多个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer,您可能需要查看配置该ignoreUnresolvablePlaceholders属性,以便您的应用程序能够干净利落地启动。
回答by Sean Patrick Floyd
Your property file location is classpath:idm.properties
您的财产文件位置是 classpath:idm.properties
This is rather unusual, it means that idm.propertiesmust be located either at the top level of WEB-INF/classesor at the top-level of one of the jars inside WEB-INF/lib. Usually it's good practice to either use a dedicated folder for properties or keep them close to the context files that use them.
这是相当不寻常的,这意味着idm.properties必须位于其中WEB-INF/classes一个罐子的顶层或顶层WEB-INF/lib。通常,为属性使用专用文件夹或将它们靠近使用它们的上下文文件是一种很好的做法。
So my suggestion is this: Is your properties file perhaps next to your context file? If so, it's not on the classpath (see this question: Is WEB-INF in the CLASSPATH?).
所以我的建议是:你的属性文件可能在你的上下文文件旁边吗?如果是这样,它不在类路径上(请参阅这个问题:WEB-INF 在 CLASSPATH 中吗?)。
The classpath:prefix maps to a ClassPathResource, but you probably need a ServletContextResource, and you'll get that from a WebApplicationContextusing the syntax without prefix:
该classpath:前缀映射到一个ClassPathResource,但你可能需要一个ServletContextResource,你会得到从WebApplicationContext使用语法不带前缀:
<context:property-placeholder location="idm.properties" />
Reference:
参考:
- The
ResourceLoader
(describes how differentApplicationContexttypes handle resources without prefix) - The
PropertyPlaceholderConfigurermechanism
(describes the<context:property-placeholder>mechanism)
- 该
ResourceLoader
(描述了不同ApplicationContext无前缀类型的处理资源) - 的
PropertyPlaceholderConfigurer机构
(描述了<context:property-placeholder>机构)
回答by axtavt
It's definitely not a problem with propeties file not being found, since in that case another exception is thrown.
找不到属性文件绝对不是问题,因为在这种情况下会引发另一个异常。
Make sure that you actually have a value with key idm.urlin your idm.properties.
确保idm.url您的idm.properties.
回答by Aravind Yarram
I still believe its to do with the props file not being located by spring. Do a quick test by passing the params as jvm params. i.e -Didm.url=....
我仍然相信这与春天没有找到道具文件有关。通过将参数作为 jvm 参数传递来进行快速测试。即-Didm.url=....
回答by Ram
make sure your properties file exist in classpath directory but not in sub folder of your classpath directory. if it is exist in sub folder then write as below classpath:subfolder/idm.properties
确保您的属性文件存在于类路径目录中,但不在类路径目录的子文件夹中。如果它存在于子文件夹中,那么写如下 classpath:subfolder/idm.properties
回答by carlos figueredo
the following property must be added in the gradle.build file
必须在 gradle.build 文件中添加以下属性
processResources {
filesMatching("**/*.properties") {
expand project.properties
}
}
Additionally, if working with Intellij, the project must be re-imported.
此外,如果使用 Intellij,则必须重新导入项目。
回答by Mukesh Koshy M
Ensure 'idm.url' is set in property file and the property file is loaded
确保在属性文件中设置了“idm.url”并加载了属性文件

