Spring的XML解析中关于DTD的路径问题-
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<bean id="hello" class="com.test.Hello" singleton="true"/>
</beans>
但是这里的DTD使用的是网络路径,如果电脑没有上网的话,那么Spring读取这个XML就一定会出问题,因为无法到达这个网络地址,解决方法就是使用本地的DTD,那么路径应该如何设置呢?一般我们的配置文件都是放在classes目录下的,也就是在Classpath下,那么是不是要把spring-beans.dtd放到classes里面呢?不行,来看一下Spring中org.springframework.beans.factory.xml.BeansDtdResolver这个类的源代码:
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.commons.logging.LogFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.springframework.core.io.Resource;
* EntityResolver implementation for the Spring beans DTD,
* to load the DTD from the Spring classpath resp. JAR file.
*
* <p>Fetches "spring-beans.dtd" from the classpath resource
* "/org/springframework/beans/factory/xml/spring-beans.dtd",
* no matter if specified as some local URL or as
* "http://www.springframework.org/dtd/spring-beans.dtd".
*
* @author Juergen Hoeller
* @since 04.06.2003
*/
public class BeansDtdResolver implements EntityResolver {
logger.debug("Trying to resolve XML entity with public ID [" + publicId +
"] and system ID [" + systemId + "]");
if (systemId != null && systemId.indexOf(DTD_NAME) > systemId.lastIndexOf("/")) {
String dtdFile = systemId.substring(systemId.indexOf(DTD_NAME));
logger.debug("Trying to locate [" + dtdFile + "] under [" + SEARCH_PACKAGE + "]");
try {
Resource resource = new ClassPathResource(SEARCH_PACKAGE + dtdFile, getClass());
InputSource source = new InputSource(resource.getInputStream());
source.setPublicId(publicId);
source.setSystemId(systemId);
logger.debug("Found beans DTD [" + systemId + "] in classpath");
return source;
}
catch (IOException ex) {
logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in classpath", ex);
}
}
// use the default behaviour -> download from website or wherever
return null;
}
throws SAXException
{
int temp = uri.indexOf (':');
// it's relative to the source file holding the URI!
// but we can't use that except when the URI is a URL.
// The entity resolver is allowed to handle URIs that are
// not URLs, so we pass URIs through with scheme intact
if (temp == -1 || uri.indexOf ('/') < temp) {
String baseURI;
if (baseURI == null)
fatal ("P-055", new Object [] { uri });
if (uri.length () == 0)
uri = ".";
baseURI = baseURI.substring (0, baseURI.lastIndexOf ('/') + 1);
if (uri.charAt (0) != '/')
uri = baseURI + uri;
else {
// We have relative URI that begins with a '/'
String baseURIScheme;
int colonIndex = baseURI.indexOf(':');
if (colonIndex == -1) {
// Base URI does not have a scheme so default to
// "file:" scheme
baseURIScheme = "file:";
} else {
baseURIScheme = baseURI.substring(0, colonIndex + 1);
}
}
// since all URIs must handle it the same.
}
// check for fragment ID in URI
if (uri.indexOf ('#') != -1)
error ("P-056", new Object [] { uri });
return uri;
}
Spring的XML解析中关于DTD的路径问题-
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
![](/assets/blank.gif)
Spring的XML解析中关于DTD的路径问题-相关推荐
- spring xercesImpl xml 解析问题
spring xercesImpl xml 解析问题 分类: spring 2009-12-29 16:11 1248人阅读 评论(0) 收藏 举报 xmlspringschema文档jbossjdk ...
- Spring的xml配置文件中tx命名空间
Spring的xml配置文件中tx命名空间 一,spring配置文件的tx命名空间 引入tx命名空间 <?xml version="1.0" encoding="U ...
- Spring OXM- 漫谈XML解析技术
概述 XML解析技术漫谈 认识XML XMl的处理技术 概述 我们先从XML各种解析技术的发展历程谈起,并介绍一些主流 O/X Mapping组件的使用方法,比如XStream.Castor.JiBX ...
- Spring之XML解析
XML解析,我们可以通过我们常用的以下代码作为入口 也许,我们习惯使用第一种加载方式,但是以前也存在 第二种加载,并且这两种加载也有差别,下面再来分析. 先分析 第二种 使用 BeanFactory ...
- XML解析中的namespace初探
初学者在解析XML文件的时候最容易遇到的问题恐怕就是XML的namespace了,本文旨在对namespace做一个简要的介绍. namespace的意义无需多说,和C++,C#等高级语言一样,XML ...
- Spring 在xml文件中配置Bean
Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...
- Spring的XML解析原理,java接口流程图
前言 爱因斯坦说过"耐心和恒心总会得到报酬的",我也一直把这句话当做自己的座右铭,这句箴言在今年也彻底在"我"身上实现了. 每一个程序员都拥有一座大厂梦,我也不 ...
- Spring的XML解析原理,java软件开发面试常见问题
前言 很多同学想进大厂,特别是刚毕业的,以及工作年限短的,不要有任何侥幸心理,踏踏实实的把基础弄扎实了,这是你通往高薪之路的唯一正确姿势. 首先从面试题做起~好了,不多说了,直接上正菜. 在这里分享一 ...
- Spring的XML解析原理,ie浏览器java插件下载
前言 Dubbo用起来就和EJB.WebService差不多,调用一个远程的服务(或者JavaBean)的时候在本地有一个接口,就像调用本地的方法一样去调用,它底层帮你实现好你的方法参数传输和远程服务 ...
最新文章
- mysql8.0_grant改变-You are not allowed to create a user with GRANT
- python编程中的if __name__ == 'main': 的作用和原理[2]
- LeetCode 289. 生命游戏(位运算)
- 阮一峰的学习Javascript闭包(Closure)
- springboot配置跨mapper.xml的全局变量
- webrtc静音检测
- PHP无法使用file_get_contents或者curl_init()函数解决办法
- HTML5+CSS——个人在线简历
- ECharts快速上手 入门教学
- 【读书笔记】向上-张自豪:清华学霸的成长之路
- kali开机密码破解
- 部署ISA2006标准版防火墙
- 15/18位身份证号码验证的正则表达式总结
- 2018-10-20-C#-从零开始写-SharpDx-应用-初始化dx修改颜色
- ubuntu打开浏览器无法上网的问题解决方法?
- 打蚊子表情包_打蚊子表情包 - 打蚊子微信表情包 - 打蚊子QQ表情包 - 发表情 fabiaoqing.com...
- 9款红包封面来了,定好闹钟领取!
- 我们已经走得太远,忘记了为什么出发
- 标准化(Normalization)
- 我失业了?| ChatGPT生信分析初体验
热门文章
- 乐高式微服务化改造(下)
- 梯度值与参数更新optimizer.zero_grad(),loss.backward、和optimizer.step()、lr_scheduler.step原理解析
- 凸函数高维性质证明(Jenson不等式)
- 2019团体程序设计天梯赛L1 L1-1	PTA使我精神焕发L1-2	6翻了L1-3	敲笨钟L1-4	心理阴影面积L1-5	新胖子公式L1-6	幸运彩票L1-7	吃鱼还是吃肉
- 基于cnn的图像二分类算法(一)
- C++程序设计实践里面石头剪刀布版王者农药实例
- 用MOBA游戏的方式来评估候选人实力
- ECMAScript6语法检查规范错误信息说明
- 一个未知的项目被声明为你的MXML文件的根。切换到源代码模式加以纠正。
- 前端学习——这十本书一定要看