spring错误 Could not resolve Spring property placeholder

34 篇文章 0 订阅

https://stackoverflow.com/questions/4779572/could-not-resolve-spring-property-placeholder


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:

<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>

Answers


You may have more than one org.springframework.beans.factory.config.PropertyPlaceholderConfigurer in your application. Try setting a breakpoint on the setLocations method 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 ignoreUnresolvablePlaceholders property so that your application will start up cleanly.

share
 
7  
I imported one application context from another one and each of them had it's own property place holder. Took me hours trying to figure it out (I had the same exception going on) and setting ignoreUnresolvablePlaceholders to true in the top app context did the trick. Thank you for writing this answer!–  forhas  Sep 23 '12 at 18:00 
 
Same problem here only in my case I had one ppc in the xml and was apparently creating another by using the @PropertySource annotation elsewhere. i got rid of the annotation and passed both props files to the ppc bean in the xml and viola. thanks for the tip. –  dev  Oct 14 '13 at 16:53 
 
So simple, but so powerfull solution for resolve all problems with placeholder configs! –  Cherry  Feb 29 '16 at 3:28
 
I had same problem but ignoreUnresolvablePlaceholders solution doesn't resolve mine. Yes, I had one more place holder configurer, but it was at my server configuration and that still prevent reading config file. I removed that place holder configurer from server configuration and all work fine. Thanks for hint. –  Mustafa Kemal  Apr 5 at 10:38 

Your property file location is classpath:idm.properties

This is rather unusual, it means that idm.properties must be located either at the top level of WEB-INF/classes or 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.

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?).

The classpath: prefix maps to a ClassPathResource, but you probably need a ServletContextResource, and you'll get that from a WebApplicationContext using the syntax without prefix:

<context:property-placeholder location="idm.properties" />

Reference:

share
 
6  
In the Spring petclinic example they put properties in src/main/resources and then reference them via classpath:app.properties. –  blahdiblah  May 7 '11 at 0:20

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.url in your idm.properties.

share
 
 
I had copy-pasted the VM options from my startup script so they were all prepended with "-D". It hadn't occurred to me to check something this obvious... Thanks! –  delucasvb  Aug 11 '16 at 13:48

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=....

share
 
 
I'm not familiar with jvm params –  mjgirl  Jan 24 '11 at 7:55
 
@mjgirl it's not that hard: -Dkey=value means setting the System Property "key" to "value" –  Sean Patrick Floyd  Jan 24 '11 at 10:39
 
Do yo mean I write that to properties file –  mjgirl  Jan 24 '11 at 11:36
1  
@mjgirl let us say u have a class called "A" with main method in it. you can pass idm.url to "A" like this "java -Didm.url=abc.com A" –  Aravind R. Yarram  Jan 24 '11 at 14:28
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值