spring-配置文件头部解释-04

spring 配置文件头部解释


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

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

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

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

  • 声明XML Schema实例,声明后可以使用schemaLocation属性。

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

  • 指定Schema的位置,这个属性必须结合命名空间使用。

  • 这个属性有两个值

    • 第一个值表示需要使用的命名空间。
    • 第二个值表示供命名空间使用的XML schema的位置。

上面配置的命名空间指定xsd规范文件,这样在进行具体配置的时候就会根据这些xsd规范文件给出相应的提示。在启动服务的时候也会根据xsd规范对配置进行校验。


在每个[xsi:schemaLocation]属性中,都是一个具有指定版本号的xsd文件。对于没有加版本号的,是使用了默认值,实质上这个默认值也是有一个具体的版本号的。

注意:[xsi:schemaLocation]属性可以指定多个,即引入多个功能。

<?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:aop="http://www.springframework.org/schema/aop"
    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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
                        
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

[xmlns:aop="http://www.springframework.org/schema/aop"]

声明前缀为aop的命名空间。

书写:有两种写法

<context:annotation-config/>|<context:annotation-config></context:annotation-config>


总结:

XML Schema命名空间避免命名冲突,将不同作用的标签分门别类(比如Spring中的tx命名空间针对事务类的标签,context命名空间针对组件的标签)。

posted @ 2020-12-07 19:48  jt_coder  阅读(151)  评论(0)    收藏  举报