关于xml文件头部xmlsn

样本:

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


    <!--开启自动扫描-->
    <context:component-scan base-package="com.huawei.test"/>

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config/jdbc.properties</value>
            </list>
        </property>
    </bean>
    <!--现在可以替换为如下方式-->
    <!--使用逗号分隔多个值-->
    <!--<context:property-placeholder location="classpath:config/jdbc.properties"/>-->

    <!--配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="url" value="${datadb.url}"/>
        <property name="username" value="${datadb.usernam}"/>
        <property name="password" value="${datadb.password}"/>
        <property name="driverClassName" value="${datadb.driverClassName}"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:mybatis/*.xml"/>
    </bean>
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.huawei.test.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <!--数据库事务控制-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--使用annotation定义事务,比如使用注解-->
    <tx:annotation-driven/>
    <mvc:annotation-driven/>
</beans>

其中,xmlsn全称为xml namespace。使用xmlsn是为了解决命名冲突的问题。

使用xmlsn方式:

  使用语法: xmlns:namespace-prefix="namespaceURI"。其中namespace-prefix为自定义前缀,只要在这个XML文档中保证前缀不重复即可;namespaceURI是这个前缀对应的XML Namespace的定义。例如样板中国的context、mv、tx等,以及后面的xml namesapce定义路径。

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

   这里定义了一个url为http://www.springframwork.org/schema/context的Namespace,并将其和前缀context绑定。这样,我们就可以在下面使用通过前缀来使用其定义的元素:

<context:component-scan base-package="com.huawei.test"/>

具体内容参考:https://my.oschina.net/itblog/blog/390001

建议再去看一下xml shema相关知识:http://www.w3school.com.cn/schema/index.asp

 

posted @ 2018-12-20 14:22  子健儿  阅读(1775)  评论(0编辑  收藏  举报