java.net.UnknownHostException:|java.net.UnknownHostException: mybatis.org_[java,mybatis,dtd,配置文件]
近几天一个bug困扰着我, 今天决定解决掉它.异常信息
没有联网的情况下, 启动项目就会报错.
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [config/*.xml]
Offending resource: URL [file:/D:/Work/JAVA/workspace/xbbsport/babasport-parent/babasport-service-product/target/babasport-service-product-1.0-SNAPSHOT/WEB-INF/classes/applicationContext-service.xml];
nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [D:\Work\JAVA\workspace\xbbsport\babasport-parent\babasport-service-product\target\babasport-service-product-1.0-SNAPSHOT\WEB-INF\classes\config\mybatis-config.xml];
nested exception is java.net.UnknownHostException: mybatis.org
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:259)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:184)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:169)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:612)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:513)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1269)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1182)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1072)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5362)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5660)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1015)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:991)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1899)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [D:\Work\JAVA\workspace\xbbsport\babasport-parent\babasport-service-product\target\babasport-service-product-1.0-SNAPSHOT\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:410)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:248)
... 69 more
Caused by: java.net.UnknownHostException: mybatis.org
分析 通过查找资料, 发现这应该是因为, 加载xml时, 加载约束进行校验时, 需要联网, 于是, 在没有联网的情况下, 就报异常, 一连上网, 立马正常.
这个约束就是:
mybatis-3-config.dtd
. 对应引用:"http://mybatis.org/dtd/mybatis-3-config.dtd"
该约束在
mybatis-config.xml
的头中.解决思路 根据网上的思路, 主要有两种办法:
- 将对应的约束放至本地, 然后引用本地约束.
- 避免tomcat扫描xml, 进行校验.
- mybatis-config.xml位于 /resources/config/ , 被spring其他配置文件导入.
- mybatis-3-config.dtd位于/resources/dtd/
- mybatis-config.xml头:
结果:
nested exception is java.io.FileNotFoundException:
\dtd\mybatis-3-config.dtd (系统找不到指定的路径。)
原因猜测:
(1)mybatis-config.xml被web.xml模糊扫描到, "web.xml 扫描到会对xml 内容进行标准校验,此时需要网络获取dtd; 如果这个文件只是配置到mybatis.xml里面的话。这个是不会去网络获取dtd 会根据本地jar 里面的dtd 进行校验。"[1]
本项目中,tomcat加载解析web.xml后, 会创建Spring的DispatcherServlet, 从而加载解析applicationContext-service.xml(因为web.xml中配有
classpath:applicationContext-service.xml
), 而applicationContext-service.xml中配有
, 而这个会扫描到resources/config/下的mybatis-config.xml. 从而会加载头文件中的约束: /dtd/mybatis-3-config.dtd. 到这里, tomcat加载不到这个约束.(2)约束需要放置resources/下, 才能被识别?
(3)只会在配置文件当前路径及其子路径下搜索?
以上3中可能性, (2)(3)的可能性应该都不大.
于是, 将mybatis-conf.xml移出/config/, 让tomcat无法直接加载解析.
说明:
- 直接加载 tomcat加载并解析xml, 本文称为
直接加载
. - 由Spring或Mybatis自身加载xml称为
非直接加载
.
果然, 猜想(1)比较合理, 避开tomcat直接扫描mybatis-conf.xml后, 正常启动了!
并且, 换成初始的网址形式, 依然成功. 说明Spring和Mybatis可以在jar包中搜寻约束, 而tomcat不能.
这样, 就可以不用修改头信息, 不用准备本地dtd约束, 放于项目目录下...(太麻烦!!)
文章图片
文件引用关系 结论 【java.net.UnknownHostException:|java.net.UnknownHostException: mybatis.org_[java,mybatis,dtd,配置文件]】再次重申那条评论(给力!):
"web.xml 扫描到会对xml 内容进行标准校验,此时需要网络获取dtd; 如果这个文件只是配置到mybatis.xml里面的话。这个是不会去网络获取dtd 会根据本地jar 里面的dtd 进行校验。"[1]
- http://blog.csdn.net/superdog007/article/details/50378612(评论)。 ? ?