zoukankan      html  css  js  c++  java
  • 0044 spring框架的applicationContext.xml的命名空间

    Spring框架中,创建bean,装配bean,事务控制等,可以用xml配置或者注解扫描的方法实现。如果用注解扫描,在xml配置中得加上

    <context:component-scan base-package="XXX" />
    <mvc:annotation-driven />
    <tx:annotation-driven transaction-manager="XXX" />
    

    但以上配置生效的前提,还要配置好相应的命名空间,比如:

    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:util="http://www.springframework.org/schema/util" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:mvc="http://www.springframework.org/schema/mvc"  
        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-4.0.xsd 
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
                            http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
                            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd 
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> 
    。。。
    </beans>
    

    那这些命名空间又是什么,以tx为例:

    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "
    

    『xmlns:tx="http://www.springframework.org/schema/tx"』声明了一个命名空间,tx就代表了『http://www.springframework.org/schema/tx』,后者看起来像个url,其实只是个字符串,之所以写成url的样子,只是为了确保唯一,就类似于包名要用域名的反写+项目名+模块名一样

    『xsi:schemaLocation="http://www.springframework.org/schema/tx【空格】http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "』指定了用于解析和校验xml文件的xsd文件的位置,那如何找到这个xsd文件呢

    把tx.jar解压缩后,在META-INF目录下有一个spring.schemas文件,打开后如下所示:

    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd=org/springframework/transaction/config/spring-tx-3.1.xsd
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd=org/springframework/transaction/config/spring-tx-3.2.xsd
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd=org/springframework/transaction/config/spring-tx-4.0.xsd
    http://www.springframework.org/schema/tx/spring-tx-4.1.xsd=org/springframework/transaction/config/spring-tx-4.1.xsd
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd=org/springframework/transaction/config/spring-tx-4.2.xsd
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd=org/springframework/transaction/config/spring-tx-4.3.xsd
    http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-4.3.xsd
    
    

    这里有xsd文件的位置的名称跟具体xsd文件所在目录的映射,到『org/springframework/transaction/config/』目录下就可以找到对应的xsd文件,打开后基本如下所示:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    
    <xsd:schema xmlns="http://www.springframework.org/schema/tx"    <!-- 与上面的xmlns:tx的值相同 --> 
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:beans="http://www.springframework.org/schema/beans"
            xmlns:tool="http://www.springframework.org/schema/tool"
            targetNamespace="http://www.springframework.org/schema/tx"
            elementFormDefault="qualified"
            attributeFormDefault="unqualified">
    
        <xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"/>
        <xsd:import namespace="http://www.springframework.org/schema/tool" schemaLocation="http://www.springframework.org/schema/tool/spring-tool-4.0.xsd"/>
    
        <xsd:annotation>
            .................
        </xsd:annotation>
    
        <xsd:element name="advice">              <!-- 引入这个命名空间后,才能用<tx:advice><tx:annotation-driven>等 -->
            .................
        </xsd:element>
    
        <xsd:element name="annotation-driven">
            .................
        </xsd:element>
    
        <xsd:element name="jta-transaction-manager">
            .................
        </xsd:complexType>
    
    </xsd:schema>
    
    

    如何引入一个命名空间呢?
    1. 声明:xmlns:tx="命名空间" 这里命名空间跟xsd文件的xmlns的值相同
    2. 在xsi:schemaLocation指定xsd文件的位置:命名空间【空格】config目录下的.schemas的映射

  • 相关阅读:
    494 Target Sum 目标和
    493 Reverse Pairs 翻转对
    492 Construct the Rectangle 构建矩形
    491 Increasing Subsequences 递增子序列
    488 Zuma Game 祖玛游戏
    486 Predict the Winner 预测赢家
    485 Max Consecutive Ones 最大连续1的个数
    483 Smallest Good Base
    Django Form组件
    Django Auth组件
  • 原文地址:https://www.cnblogs.com/sonng/p/6582439.html
Copyright © 2011-2022 走看看