当前位置: 首页 > 面试题库 >

无法解析Spring属性占位符

郭兴平
2023-03-14
问题内容

我有我的配置:

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

我得到错误

    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

我知道这可能缺少属性文件,但是我在类路径中恰好有它。有什么不见了?

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

问题答案:

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的应用程序中可能有多个。尝试setLocations在超类的方法上设置一个断点,看看在应用程序启动时是否多次调用了该断点。如果不止一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer,则可能需要查看配置ignoreUnresolvablePlaceholders属性,以便你的应用程序可以正常启动。



 类似资料:
  • 问题内容: 我对春天还很陌生,所以请问这是一个愚蠢的问题。当我尝试启动程序时,出现以下错误:。执行以下代码时,将引发错误: 资源文件夹中存在一个名为的属性文件,其中包含主机和端口的信息。我不确定在哪里定义(如果有的话)。也许甚至没有定义,这就是问题所在。我需要将其更改为类似的东西还是缺少其他内容? 问题答案: 您没有正确读取属性文件。propertySource应该将参数传递为:或。将注释更改为:

  • 我的数据库配置类: 和AppConfig: } 嗨,我上面有个错误,我不知道怎么修复,你能帮我吗?在添加这个bean之前,我的项目运行良好: }

  • 我有个例外 我也尝试过使用JarLoader和PropertiesLauncher,但运气并不好。 我确实在application.properties中定义了属性sysm.client.api.path,但为了更好地衡量,我还将它作为-d参数-dsysm.client.api.path=my-path添加到命令行中。 注意:IntelliJ没有将其作为a-jar运行;相反,它在一个大型类路径命令

  • 我不明白为什么不能在Spring-Boot中向application.properties文件中注入值。外部属性添加到logging.file变量中。我有一个application.properties文件,看起来如下所示 具有相应的Spring-boot应用程序类 请注意,如果我自己注入服务器端口号,那么在注入和启动应用程序时没有任何问题。 我在这个问题上兜圈子,弄不清自己做错了什么。

  • 问题内容: 我有一个使用Logback的Spring Boot控制台应用程序。所有属性(针对应用程序和Logback)都被外部化为类路径中的标准application.properties文件。在应用程序本身中可以很好地拾取这些属性,但在logback.xml文件中不会拾取这些属性。在Spring Boot启动之前,似乎好像处理了logback.xml,因此未处理EL占位符。 以FileNameP

  • 我有一个使用Logback的Spring Boot控制台应用程序。所有属性(应用程序和日志)都外部化到类路径中的标准application.properties文件中。这些属性在应用程序本身中可以很好地提取,但在logback.xml文件中不能提取。看起来好像是在Spring Boot启动之前处理了logback.xml,因此没有处理EL占位符。 使用FileNamePattern作为示例,在ap