前言
开启Tomcat服务器时报错
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.zh.config.SpringConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/jdbc.properties]
at org.springframework.context.annotation.ConfigurationClassParser.parse (ConfigurationClassParser.java:189)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions (ConfigurationClassPostProcessor.java:319)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry (ConfigurationClassPostProcessor.java:236)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors (PostProcessorRegistrationDelegate.java:280)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors (PostProcessorRegistrationDelegate.java:96)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors (AbstractApplicationContext.java:707)
at org.springframework.context.support.AbstractApplicationContext.refresh (AbstractApplicationContext.java:533)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext (ContextLoader.java:401)
at org.springframework.web.context.ContextLoader.initWebApplicationContext (ContextLoader.java:292)
at org.springframework.web.context.ContextLoaderListener.contextInitialized (ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart (StandardContext.java:4797)
at org.apache.catalina.core.StandardContext.startInternal (StandardContext.java:5291)
at org.apache.catalina.util.LifecycleBase.start (LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call (ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call (ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run (FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
at java.lang.Thread.run (Thread.java:748)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/jdbc.properties]
at org.springframework.web.context.support.ServletContextResource.getInputStream (ServletContextResource.java:159)
at org.springframework.core.io.support.EncodedResource.getInputStream (EncodedResource.java:159)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties (PropertiesLoaderUtils.java:99)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties (PropertiesLoaderUtils.java:73)
at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties (PropertiesLoaderUtils.java:59)
at org.springframework.core.io.support.ResourcePropertySource.<init> (ResourcePropertySource.java:67)
at org.springframework.core.io.support.DefaultPropertySourceFactory.createPropertySource (DefaultPropertySourceFactory.java:37)
at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource (ConfigurationClassParser.java:463)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass (ConfigurationClassParser.java:280)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass (ConfigurationClassParser.java:250)
at org.springframework.context.annotation.ConfigurationClassParser.parse (ConfigurationClassParser.java:207)
at org.springframework.context.annotation.ConfigurationClassParser.parse (ConfigurationClassParser.java:175)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions (ConfigurationClassPostProcessor.java:319)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry (ConfigurationClassPostProcessor.java:236)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors (PostProcessorRegistrationDelegate.java:280)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors (PostProcessorRegistrationDelegate.java:96)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors (AbstractApplicationContext.java:707)
at org.springframework.context.support.AbstractApplicationContext.refresh (AbstractApplicationContext.java:533)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext (ContextLoader.java:401)
at org.springframework.web.context.ContextLoader.initWebApplicationContext (ContextLoader.java:292)
at org.springframework.web.context.ContextLoaderListener.contextInitialized (ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart (StandardContext.java:4797)
at org.apache.catalina.core.StandardContext.startInternal (StandardContext.java:5291)
at org.apache.catalina.util.LifecycleBase.start (LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call (ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call (ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run (FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
at java.lang.Thread.run (Thread.java:748)
意思是文件无法找到,无法打开文件ServletContext
的资源文件
解决方法
找到SpringConfig.class
文件,定位到注解@PropertySource
jdbc.properties
文件在resources
文件夹下,路径没错
于是,在jdbc.properties
前加上classpath:
即可,报错消失