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

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>
    
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值