关于spring配置文件xml文档的schema约束

1. 配置文件示例

<?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: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">

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath*:mybatis-mappers/*.xml"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.acc.quota.pool.mappers" />
    </bean>

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- enable transaction annotation support -->
    <tx:annotation-driven transaction-manager="txManager" />

</beans>

 

2. 解释

声明默认的名称空间:xmlns="http://www.springframework.org/schema/beans"

声明实例的名称空间:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ;xmlns:tx="http://www.springframework.org/schema/tx"

(引入多个schema文件需要使用别名,xsi,tx),将对应xsi、tx 与该名称空间绑定

使用xsi:schemaLocation指定名称空间和模式位置的关系

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

 

xml的schema里有namespace,可以给他起个别名,比如常见的: xmlns:tx="http://www.springframework.org/schema/tx"

通常情况下,namespace对应的url是一个存放xsd的地址,如果没有提供schemaLocation,spring的xml解析器会从namsespace里加载xsd文件 

 

3. 避免使用带版本号的xsd文件(spring已解该问题)

spring默认在启动时加载xsd文件验证xml,如果断网或开源软件切换域名,易导致应用启动不了,不带版本号,默认使用最新版本。spring提供一种机制,默认从本地加载xsd文件

打开xxx.jar -> spring.schemas, spring.handlers, 再打开 org/springframework/context/config/  可以看到所有版本的xsd文件都在本地,通过spring.schemas映射,优先从本地加载xsd文件。旧版本xsd文件放入,避免升级了spring版本,配置文件里还在用旧版本的xsd文件,断网后,启动不了。

 

4. xml配置,找不到对应的类文件

可以看下是不是jar包依赖的问题,如果依赖版本混乱,容易导致该问题

 

5. xml的schema约束

 xml的schema约束,定义了xml的文档结构、内容和语法

-所有的标签和属性,都需要schema来定义

-所有的schema文件都需要一个ID(namespace)和值(url,通常是这个xml的xsd文件地址)

 

6. bean标签认识

-如何将自己的xml文件注入spring容器

在web.xml中的context-param标签中配置xml文件,通过listener标签将配置的文件注入到spring容器

-如果把自己的类注入到spring容器,配置的是接口还是实现类

在spring容器能加载到的xml里,配置bean就把自定义类加入到了spring容器,注入的是类,不是接口

-依赖注入的三种方法

属性注入

<bean id="helloWorld" class="com.test.spring.beans.HelloWorld">
        <property name="name" value="Spring"></property>
</bean>

构造方法注入

<bean id="car" class="com.test.spring.beans.Car">
        <constructor-arg value="Audi" index="0"></constructor-arg>
        <constructor-arg value="ShangHai" index="1"></constructor-arg>
        <constructor-arg value="300000" type="double"></constructor-arg>
</bean>

工厂方法注入

 

参考: https://www.cnblogs.com/justfuck/p/5930773.html

https://blog.csdn.net/hengyunabc/article/details/22295749

https://www.jianshu.com/p/fdf603d18ad7

 

 

 

  

 

posted on 2020-03-25 15:18  wangsong412  阅读(540)  评论(0编辑  收藏  举报