spring 整合Mybatis
运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse
错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist
错误原因:找不到我的springmvc.xml,在下面web.xml中是我引用路径,网上找到问题classpath指向路径不是resource路径,所以一直找不到我的xml文件,
classpath:到你的class路径中查找文件,
classpath*:不仅包含class的路径,还包括jar文件中(class路径)进行查找
解决办法:在classpath后面在上“*” 可解决问题,“classpath*:springmvc.xml”
错误代码:
1 2017-04-05 08:15:10 [org.springframework.web.servlet.DispatcherServlet]-[INFO] FrameworkServlet 'springmvc': initialization started 2 2017-04-05 08:15:10 [org.springframework.web.context.support.XmlWebApplicationContext]-[INFO] Refreshing WebApplicationContext for namespace 'springmvc-servlet': startup date [Wed Apr 05 08:15:10 CST 2017]; parent: Root WebApplicationContext 3 2017-04-05 08:15:10 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [springmvc.xml] 4 2017-04-05 08:15:10 [org.springframework.web.servlet.DispatcherServlet]-[ERROR] Context initialization failed 5 org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [springmvc.xml]; nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be opened because it does not exist 6 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) 7 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) 8 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) 9 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) 10 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) 11 at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) 12 at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) 13 at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131) 14 at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537) 15 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451) 16 at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647) 17 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598) 18 at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661) 19 at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517) 20 at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458) 21 at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138) 22 at javax.servlet.GenericServlet.init(GenericServlet.java:160) 23 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280) 24 at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193) 25 at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865) 26 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136) 27 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 28 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 29 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 30 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 31 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 32 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 33 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 34 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 35 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) 36 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 37 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 38 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 39 at java.lang.Thread.run(Thread.java:722) 40 Caused by: java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be opened because it does not exist 41 at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157) 42 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328) 43 ... 33 more
相关代码:
web.xml 配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>Springmvc_Mybits_store</display-name> 4 <!-- 加载spring容器 --> 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>classpath:applicationContext-*.xml</param-value> 8 </context-param> 9 <!-- 监听器,扫描通配符 --> 10 <listener> 11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 12 </listener> 13 <!-- 前端控制器配置 --> 14 <servlet> 15 <servlet-name>springmvc</servlet-name> 16 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 17 <!--加载springmvc.xml配置--> 18 <init-param> 19 <param-name>contextConfigLocation</param-name> 20 <param-value>classpath:springmvc.xml</param-value> 21 </init-param> 22 </servlet> 23 <!-- servlet映射 --> 24 <servlet-mapping> 25 <servlet-name>springmvc</servlet-name> 26 <url-pattern>*.action</url-pattern> 27 </servlet-mapping> 28 <welcome-file-list> 29 <welcome-file>index.html</welcome-file> 30 <welcome-file>index.htm</welcome-file> 31 <welcome-file>index.jsp</welcome-file> 32 <welcome-file>default.html</welcome-file> 33 <welcome-file>default.htm</welcome-file> 34 <welcome-file>default.jsp</welcome-file> 35 </welcome-file-list> 36 </web-app>