在ssh中 无法读取数据源配置文件 dataSource.properties 的问题

举报
码农小胖哥 发表于 2022/03/31 00:21:29 2022/03/31
【摘要】 错误 十月 11, 2016 5:32:41 下午 org.apache.catalina.core.ApplicationContext log信息: Initializing Spring root WebApplicationContext十月 11, 2016 5:32:42 下午 org.apache.catali...
  • 错误



  
  1. 十月 11, 2016 5:32:41 下午 org.apache.catalina.core.ApplicationContext log
  2. 信息: Initializing Spring root WebApplicationContext
  3. 十月 11, 2016 5:32:42 下午 org.apache.catalina.core.StandardContext listenerStart
  4. 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  5. org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/dataSource.properties]
  6. at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151)
  7. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
  8. at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
  9. at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
  10. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
  11. at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
  12. at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
  13. at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
  14. at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
  15. at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
  16. at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
  17. at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
  18. at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
  19. at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
  20. at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1551)
  21. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  22. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  23. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  24. at java.lang.reflect.Method.invoke(Method.java:497)
  25. at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
  26. at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
  27. at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
  28. at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:622)
  29. at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:569)
  30. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  31. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  32. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  33. at java.lang.reflect.Method.invoke(Method.java:497)
  34. at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
  35. at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
  36. at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
  37. at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466)
  38. at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
  39. at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307)
  40. at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1399)
  41. at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:828)
  42. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  43. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  44. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  45. at java.lang.reflect.Method.invoke(Method.java:497)
  46. at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
  47. at sun.rmi.transport.Transport$1.run(Transport.java:200)
  48. at sun.rmi.transport.Transport$1.run(Transport.java:197)
  49. at java.security.AccessController.doPrivileged(Native Method)
  50. at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
  51. at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
  52. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
  53. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$254(TCPTransport.java:683)
  54. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/444230198.run(Unknown Source)
  55. at java.security.AccessController.doPrivileged(Native Method)
  56. at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
  57. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  58. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  59. at java.lang.Thread.run(Thread.java:745)
  60. Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/dataSource.properties]
  61. at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117)
  62. at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143)
  63. at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
  64. at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
  65. at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
  66. at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:142)
  67. ... 53 more
  68. 十月 11, 2016 5:32:42 下午 org.apache.catalina.core.ApplicationContext log
  69. 信息: Closing Spring root WebApplicationContext
  70. <span style="font-size:14px;"> </span>

  • 分析原因  

单纯代码测试中   这个问题不会出现   一旦部署到容器中  就会抛出上面的异常


因为 在  web.xml文件中 我们要在应用启动就加载 spring的配置文件    所以会在web.xml中 配置相关信息如下




  
  1. <!--应用启动就加载配置文件-->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath*:spring_cfg.xml</param-value>
  5. </context-param>


这样   可以在加载时 就读取spring的配置文件  当读到 dataSource.properties时     我们很容易在spring中的配置文件中 进行如下的错误配置




  
  1. <!--加载数据源配置文件 此处要解决路径问题 避免部署时出现问题-->
  2. <context:property-placeholder location="dataSource.properties"/>


这时 io无法正确加载    dataSource.properties      归根到底就是路径问题     

所以   如法炮制     在  dataSource.properties 前也加上  classpath*:


  
  1. 正确的方式为:
  2. <!--加载数据源配置文件 此处要解决路径问题 避免部署时出现问题-->
  3. <context:property-placeholder location="classpath:dataSource.properties"/>






文章来源: felord.blog.csdn.net,作者:码农小胖哥,版权归原作者所有,如需转载,请联系作者。

原文链接:felord.blog.csdn.net/article/details/52790099

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。