spring中的命名空间以及xml详解

来源:互联网 发布:php检测文件类型 编辑:程序博客网 时间:2024/04/29 03:16

折腾了好久,还是没有完全弄清楚eclipse中xml提示到底是怎么搞的,就算在xml Catalog中加了映射还是不行,现在问题还有几个

1 那些帮助信息到底是从哪里来的,从 jar的schema中? 从网上 ? 还是本地的xml catalog映射 ? 以及他们的优先级


搞不清的问题以后再慢慢了解,先把弄清楚了的问题记录下来

自定义spring的命名空间这一功能是在spring2之后引进来的,spring的容器启动时回去寻找jar包下面的META-INF,看里面是否有spring.schema或者是spring.handlers,这个handler文件里面就定义了命名空间以及处理这个命名空间的java类

http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandlerhttp\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandlerhttp\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandlerhttp\://www.springframework.org/schema/task=org.springframework.scheduling.config.TaskNamespaceHandlerhttp\://www.springframework.org/schema/cache=org.springframework.cache.config.CacheNamespaceHandler
这个是spring的context包中spring.handlers的内容,里面清楚的写出了命名空间以及对应的Java类,

http\://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsdhttp\://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsdhttp\://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsdhttp\://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.1.xsdhttp\://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsdhttp\://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsdhttp\://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsdhttp\://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsdhttp\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-3.1.xsdhttp\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsdhttp\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsdhttp\://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsdhttp\://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsdhttp\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-3.1.xsdhttp\://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsdhttp\://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsdhttp\://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-3.1.xsdhttp\://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsdhttp\://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-3.1.xsd
这里是spring.schema中的内容,对应着具体schema的位置,个人认为运行时的验证应该是根据这里来获取schema验证的。


今天好像搞清楚了,xml提示的加载机制,如果写了命名空间的schemaLocation,那么就会去那里寻找,如

http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
如果我们没有指定schemaLocation,那么就会从本地去寻找,我们手动添加的映射就有用了,经过测试,好像spring核心包的提示不是通过上述的规则寻找的,应该是直接扫描的jar包里面的schema(暂时这么认为,具体还有待考证)


这篇文章讲了自定义命名空间http://mozhenghua.iteye.com/blog/1830842


为了进一步了解xml和schema,于是需要深入了解

关于Node(节点)

这是w3c文档给出的解释

According to the DOM, everything in an XML document is a node.

The DOM says:

  • The entire document is a document node (整个Document是一个document节点)
  • Every XML element is an element node(每个xml节点是一个元素阶段)
  • The text in the XML elements are text nodes (xml元素中的文本是文本节点)
  • Every attribute is an attribute node(每个属性是属性节点)
  • Comments are comment nodes (每个注释是注释节点)
更多介绍参考文档    点击查看

具体对xml的操作可以看www.mkyong.com的教程,这里记录一下我遇到过的问题

1 我在用DOM解析xml后,使用getElementById("")来获取某个元素,发现获取不到,在stackoverflow中找了两篇文章

http://stackoverflow.com/questions/3423430/java-xml-dom-how-are-id-attributes-special

http://stackoverflow.com/questions/19929107/xml-id-attribute-to-work-with-javas-getelementbyid

基本上原因是这样,如果我的元素是这样<student id="kevin"></student>, 因为我们所写的id,DOM并不认为他是id属性,也就是说id属性是可以单独定义的,比如我们可以将

aa这个定义为id属性,如果有一个节点是<student aa="kevin"></student> , 那么document.getElementById("kevin") 就可以获取这个节点了

你还可以通过Attr.isId()这个方法来查看某个属性到底是不是id属性。





0 0