Spring的XML配置文件的头部文件

来源:互联网 发布:linux 新建log文件 编辑:程序博客网 时间:2024/06/05 14:18

Spring的XML配置文件的头部文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context.xsd         http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx.xsd"></beans>

头部配置文件的说明

<?xml version="1.0" encoding="UTF-8"?>

version="1.0"声明使用的xml的版本是1.0

encoding="UTF-8"声明用xml传输数据时使用的字符编码,假如文档里面有中文而编码方式不是UTF-8,传输过去再解码就会导致中文乱码

xmlns="http://www.springframework.org/schema/beans"

声明xml文件默认的命名空间,表示未使用其他命名空间的所有标签的默认命名空间

XML命名空间(Namespace)提供避免元素命名冲突的方法,将命名空间和元素名、属性连在一起使用,这样就不会同其他同名元素混淆(类似于Java中包的作用)。在XML中,采用现成的、在全球范围唯一的域名,即URL作为Namespace

http://www.springframework.org/schema/beans就是一个命名空间,xmlns:xxx是给该命名空间起的一个前缀,所有带有相同前缀的子元素都会与同一个命名空间相关联

这里由于没有起前缀,所以在XML中没有加前缀的子元素就是使用的http://www.springframework.org/schema/beans里的元素

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

声明XMLSchema实例,声明后就可以使用schemaLocation属性

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

为上面配置的命名空间指定xsd规范文件,这样你在进行下面具体配置的时候就会根据这些xsd规范文件给出相应的提示,比如说每个标签是怎么写的,都有些什么属性是都可以智能提示的,以防配置中出错而不太容易排查,在启动服务的时候也会根据xsd规范对配置进行校验

标签的书写技巧

  • 有时候一些标签没有内嵌了,就会有以下两种写法,其效果是一样的
  • 为了美观,通常选择前者的写法

    <context:annotation-config/><context:annotation-config></context:annotation-config>
原创粉丝点击