最近跑项目一直被一个问题所困扰
在没有联网(或者网络不佳)的情况下,运行项目会报如题所示的错误,详细见以下报错信息
先给出结论,这种情况是由于配置web.xml
时,使tomcat直接扫描了mybatis-config.xml
文件,此时tomcat需要联网校验,所以报错。
Connected to server
[2022-04-13 10:12:21,006] Artifact crm_ssm:war exploded: Artifact is being deployed, please wait…
10:12:22.277 INFO org.springframework.web.context.ContextLoader.initWebApplicationContext @271 :-> Root WebApplicationContext: initialization started
10:12:22.802 ERROR org.springframework.web.context.ContextLoader.initWebApplicationContext @313 :-> Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/Users/musou/MrStone/Java/项目/CRM(ssm版)/crm_ssm/target/crm_ssm/WEB-INF/classes/config/mybatis-config.xml]; nested exception is java.net.UnknownHostException: mybatis.org
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:413) ~[spring-beans-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:338) ~[spring-beans-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310) ~[spring-beans-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) ~[spring-beans-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224) ~[spring-beans-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195) ~[spring-beans-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) ~[spring-web-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) ~[spring-web-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130) ~[spring-context-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:638) ~[spring-context-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) ~[spring-context-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:401) ~[spring-web-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:292) ~[spring-web-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103) ~[spring-web-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4705) ~[catalina.jar:8.5.63]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5171) ~[catalina.jar:8.5.63]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[catalina.jar:8.5.63]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743) ~[catalina.jar:8.5.63]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719) ~[catalina.jar:8.5.63]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705) ~[catalina.jar:8.5.63]
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1663) ~[catalina.jar:8.5.63]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_281]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_281]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_281]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_281]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:286) ~[tomcat-coyote.jar:8.5.63]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[?:1.8.0_281]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[?:1.8.0_281]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482) ~[catalina.jar:8.5.63]
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431) ~[catalina.jar:8.5.63]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_281]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_281]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_281]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_281]
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:286) ~[tomcat-coyote.jar:8.5.63]
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) ~[?:1.8.0_281]
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) ~[?:1.8.0_281]
at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468) ~[?:1.8.0_281]
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468) ~[?:1.8.0_281]
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76) ~[?:1.8.0_281]
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309) ~[?:1.8.0_281]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_281]
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408) ~[?:1.8.0_281]
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829) ~[?:1.8.0_281]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_281]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_281]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_281]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_281]
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357) ~[?:1.8.0_281]
at sun.rmi.transport.Transport$1.run(Transport.java:200) ~[?:1.8.0_281]
at sun.rmi.transport.Transport$1.run(Transport.java:197) ~[?:1.8.0_281]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_281]
at sun.rmi.transport.Transport.serviceCall(Transport.java:196) ~[?:1.8.0_281]
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573) ~[?:1.8.0_281]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834) ~[?:1.8.0_281]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688) ~[?:1.8.0_281]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_281]
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687) [?:1.8.0_281]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_281]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_281]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_281]
Caused by: java.net.UnknownHostException: mybatis.org
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:196) ~[?:1.8.0_281]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394) ~[?:1.8.0_281]
at java.net.Socket.connect(Socket.java:606) ~[?:1.8.0_281]
at java.net.Socket.connect(Socket.java:555) ~[?:1.8.0_281]
at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[?:1.8.0_281]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) ~[?:1.8.0_281]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) ~[?:1.8.0_281]
at sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[?:1.8.0_281]
at sun.net.www.http.HttpClient.New(HttpClient.java:339) ~[?:1.8.0_281]
at sun.net.www.http.HttpClient.New(HttpClient.java:357) ~[?:1.8.0_281]
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1226) ~[?:1.8.0_281]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1162) ~[?:1.8.0_281]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1056) ~[?:1.8.0_281]
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:990) ~[?:1.8.0_281]
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1570) ~[?:1.8.0_281]
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:646) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1302) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startDTDEntity(XMLEntityManager.java:1268) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.setInputSource(XMLDTDScannerImpl.java:264) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1189) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1073) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:974) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:507) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:867) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:796) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:142) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:247) ~[?:1.8.0_281]
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339) ~[?:1.8.0_281]
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:77) ~[spring-beans-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:432) ~[spring-beans-5.2.18.RELEASE.jar:5.2.18.RELEASE]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390) ~[spring-beans-5.2.18.RELEASE.jar:5.2.18.RELEASE]
... 60 more
如图所示是mybatis-config.xml
文件的头部内容,其中http://mybatis.org/dtd/mybatis-3-config.dtd
就是tomcat需要加载的dtd文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
解决办法一般有二
mybatis-config.xml
第一种办法可以从网上下载dtd文件,然后放在资源目录下,修改mybatis-config.xml
文件如下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
第二种办法(推荐),我们可以将配置文件的扫描交给spring来处理,我发生题示错误的原因是将web.xml
配置为如下信息
<!--配置文件的存放位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/*.xml</param-value>
</context-param>
可以改为以下内容,这样在tomcat启动时只会扫描applicationContext.xml
的内容
<!--配置文件的存放位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
然后在applicationContext.xml
中引入 <import resource="spring-mybatis.xml"/>
,配置spring-mybatis.xml
如下,引用mybatis-config.xml
的内容
<!--配置sqlSession,与MyBatis整合-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源-->
<property name="dataSource" ref="druidDataSource"/>
<!--定义哪些路径下的文件具有别名-->
<property name="typeAliasesPackage" value="com.ml.web.entity"/>
<!--定义MyBatis配置文件的位置-->
<property name="configLocation" value="classpath:config/mybatis-config.xml"/>
</bean>
这样, 问题就被完美解决了。