Spring beans DTD and XMLNS

15

当我创建一个Spring项目时,我经常遇到XMLNS的问题。什么是XMLNS?这些究竟是什么?

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"

这些的参考文献在哪里可以获得?(xmlns:xsi 和 xsi:schemeLocation 的资源) 是否有在线手册可供查询?我似乎找不到它们。

注意 当我说参考文献时,我的意思是它们的正确网址。

更新

在哪里可以查看Spring Bean、Spring事务、Spring MVC等XML命名空间及其模式位置?


1
可能有重复:https://dev59.com/fHM_5IYBdhLWcg3w4njb - Kevin Bowersox
3个回答

17

这里有一个很好的解释:xsi:schemaLocation的用途是什么?

这是Spring文档中关于xsd配置的内容:http://static.springsource.org/spring/docs/current/spring-framework-reference/html/xsd-config.html

注意:现在Spring建议不要在xsd中包含版本号,除非特别需要,因此你应该这样写:

xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd"

不是

xsi:schemaLocation="http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"

"xmlns"定义当前元素的命名空间。

"xmlns:aop"定义当前元素内具有前缀"aop:"的元素的命名空间。


3
这些行用于设置XML文档的命名空间。根据您在XML文件中使用的标记,您需要在顶部(并引用正确的模式)设置命名空间,以便使XML有效。例如,如果您在bean定义中使用标记,则需要在文件顶部引用aop模式:xmlns:aop="http://www.springframework.org/schema/aop" 如果您没有使用该标记,则不需要添加。对于导入的任何命名空间,请确保在“xsi:schemaLocation”标记中添加对模式的引用,如下所示:xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 我建议查看一个Spring应用程序样例,因为它应该具有运行所需的最少要求。

他们没有在线参考资料吗?让用户猜测不是更好的选择。 - user962206
在XML中,命名空间ID只是本地于XML文件的键。通常选择与之匹配的键(例如,在AOP模式下使用aop),但如果您愿意,也可以使用fdsa;您只需在该模式的所有XML标记中使用fdsa前缀即可。 - chrylis -cautiouslyoptimistic-

1
就“文档在哪里记录”的回答而言,我认为这取决于每个项目。以Spring为例,该项目文档确实包含了此信息的参考。例如,查看Spring Framework 3.2.x的xsd-config部分:附录E. 基于XML Schema的配置

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接