(spring-第2回)Spring的Schema,基于XML的配置(在IoC容器中装配Bean的前奏片)

要深入了解Spring机制,首先需要知道Spring是怎样在IoC容器中装配Bean的。而了解这一点的前提是,要搞清楚Spring基于Schema的Xml配置方案。

在深入了解之前,必须要先明白几个标签的意思(我会逐步引导读者理解,刚开始的懵懂无所谓,读者自会渐入佳境。初极狭,才通人。复行数十步,豁然开朗。)。

  1. 什么是XML Schema?

  用来描述 XML文档的结构,也被简称为XSD(XML Schema Definition),是一些规则的集合。(方式:通过定义schema文件 如 spring-tx-3.0.xsd)

  2. xmlns?

  命名空间是W3C推荐标准提供的一种统一命名XML文档中的元素和属性的机制。

  3.xsd文件?

  使用XML w3c 标准命名空间中规定的元素和属性编写的以targetNamespace作为{目标命名空间}的XML文件,能够约束引入此{目标命名空间}定义的元素和属性的XML文件。

  4.targetNamespace?

  目标命名空间,它的主要作用是指明Schema定义的元素的命名空间。

亲,是不是蒙圈了?我举个栗子你感受一下,下面是一个note.xsd文件示例:

代码001 

1 <?xml version="1.0"?>
 2 <xs:schema
 3 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 4 targetNamespace="http://www.w3school.com.cn">
 5 <xs:element name="note">
 6     <xs:complexType>
 7       <xs:sequence>
 8     <xs:element name="to" type="xs:string"/>
 9     <xs:element name="from" type="xs:string"/>
10     <xs:element name="heading" type="xs:string"/>
11     <xs:element name="body" type="xs:string"/>
12       </xs:sequence>
13     </xs:complexType>
14 </xs:element>
15 </xs:schema>

这个xsd文件你就认为它是个有约束力的文件,它将约束后面定义的与bean相关的xml,

第3行的xmlns就是一个统一的规则,它就是"法",平民百姓可以自由支配自己,但必须遵循这个"法"的管制。

5-14行就是约束的方法,第五行"<"后面的"xs"是这个约束的小名,对应着第3行"xmlns"后面的"xs",而约束文件的全名就是第三行后面的以http开始的链接。(这个全名其实可以随意定义,但是一般情况下用网站目录来命名,一是便于区分,二是体现出本文件在服务器中的组织架构关系)。

第4行是本约束规则要约束哪个文件?targetNamespace后面就是被约束文件的大名。通缉令上写上大名:抓捕汉奸王二麻子,然后捕头就按图索骥,寻找王二麻子这个人。下面是王二麻子:

代码002

1 <?xml version="1.0"?>
2 <note xmlns="http://www.w3school.com.cn"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.w3school.com.cn                http://www.w3school.com.cn /note.xsd">
5 <to>George</to>
6 <from>John</from>
7 <heading>Reminder</heading>
8 <body>Don‘t forget the meeting!</body>
9 </note>

看到木有,第2行xmlns后面,行不改名,坐不改姓,http://www.w3school.com.cn这就是这个配置文件内定义的大名(代号王二麻子)。5到8行是这个bean配置文件的属性,这些属性遵循的规则就是前面讲到的代码001的5-14行约束。

这一行的xmlns后面本来也有个冒号+小名的,不写表示默认命名空间,spring中bean的定义都使用默认命名空间。

那么这个xmlns:xsi又是什么呢?兄弟你记住一点,xmlns就是"法",它后面的东西都是定义的规则的大名和小名。

你看,代码001中第三行说明,规则的小名叫xs,规则的大名叫http://www.w3.org/2001/XMLSchema。

那么,代码002中,xsi就是规则小名,后面的http://www.w3.org/2001/XMLSchema-instance就是规则的大名了。(跟代码001不同的是,001是自定义的,而xsi是spring原配的。)

第4行是调用xsi规则的schemaLocation子规则,这个规则是寻找具体约束它的文件。(通过后面的链接就可以找到代码001这个文件。)

与xmlns:xsi类似,spring内置了很多Schema约束文件,如:beans、aop、tx、mvc、util等等,不一而足。

有了上面的栗子,spring项目里具体的xml文件就有所了解了,再来看,下面是一个小栗子的配置文件:

代码003

1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10        http://www.springframework.org/schema/context
11        http://www.springframework.org/schema/context/spring-context-3.0.xsd
12        http://www.springframework.org/schema/tx
13        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14        http://www.springframework.org/schema/aop
15        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
16
17     <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
18     <context:component-scan base-package="com.baobaotao.dao"/>
19     <context:component-scan base-package="com.baobaotao.service"/>
20
21     <!-- 配置数据源 -->
22     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
23         destroy-method="close"
24         p:driverClassName="com.mysql.jdbc.Driver"
25         p:url="jdbc:mysql://localhost:3306/sampledb"
26         p:username="root"
27         p:password="2009118293zjl" />
28
29     <!-- 配置Jdbc模板  -->
30     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
31         p:dataSource-ref="dataSource" />
32
33     <!-- 配置事务管理器 -->
34     <bean id="transactionManager"
35         class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
36         p:dataSource-ref="dataSource" />
37
38     <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
39     <aop:config proxy-target-class="true">
40         <aop:pointcut id="serviceMethod"
41             expression=" execution(* com.baobaotao.service..*(..))" />
42         <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
43     </aop:config>
44     <tx:advice id="txAdvice" transaction-manager="transactionManager">
45         <tx:attributes>
46             <tx:method name="*" />
47         </tx:attributes>
48     </tx:advice>
49 </beans>

代码003中,3-7行都是spring的内置xsd,8-15行是调用xsi的schemaLocation去寻找具体的xsd位置。18-48行都是为各个规则定义了一些约束对象,王二麻子一箩筐。

如果你的电脑连了网,在myeclipse中打开这个xml的时候,你可以随便挑一个规则,鼠标指在后面的链接上,按住ctrl键,点击链接,比如点击http://www.springframework.org/schema/tx/spring-tx-3.0.xsd这个链接,它会直接从内置浏览器通过访问链接打开这个xsd文件,下面是打开后的部分代码:

 1 <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
 2 - <xsd:schema xmlns="http://www.springframework.org/schema/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">
 3   <xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" />
 4   <xsd:import namespace="http://www.springframework.org/schema/tool" schemaLocation="http://www.springframework.org/schema/tool/spring-tool-3.0.xsd" />
 5 - <xsd:annotation>
 6 - <xsd:documentation>
 7 - <![CDATA[
 8     Defines the elements used in the Spring Framework‘s declarative
 9     transaction management infrastructure.
10
11
12   ]]>
13   </xsd:documentation>
14   </xsd:annotation>
15 - <xsd:element name="advice">

这个就是tx约束文件,读者可以大概看一下前面几行的意思,这里不做详解。

但是有个问题,一般情况下开发项目怎么可能保证联网呢?不联网的情况下这些约束如何生效?

其实,spring的jar包中都放了这些xsd文件。比如用压缩工具打开org.springframework.beans-3.0.5.RELEASE.jar这个jar包,

这里面都有。

Spring除了基于XML的配置,还有基于Bean,基于注解的配置。但是基于XML的配置功能最强,是基础,是必须首先要了解的。

天道酬勤。共勉。

  

  

时间: 2024-08-24 01:02:19

(spring-第2回)Spring的Schema,基于XML的配置(在IoC容器中装配Bean的前奏片)的相关文章

spring在IoC容器中装配Bean详解

1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean实例放入bean缓存池: 应用程序使用bean. 1.2.基于xml的配置 (1)xml文件概述 xmlns------默认命名空间 xmlns:xsi-------标准命名空间,用于指定自定义命名空间的schema文件 xmlns:xxx="aaaaa"-------自定义命名空间,xx

spring IOC容器中装配Bean(XML)

——基于XML的配置 依赖注入 spring 依赖注入的方式有,属性注入和构造函数注入,还有不常用到的工厂方法注入. (1)属性注入 属性注入要求 Bean 提供默认的构造函数,并为需要的属性提供 set 方法,spring 先调用 Bean 的默认构造函数实例化 Bean 对象,然后通过反射的方法调用 set 方法注入属性值.如下简单的范例: public class Phone { private int id; private String name; private double pri

[原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 -----------------------------------------------------------------------------------------------------------------

Spring(1)--在IoC容器中添加bean

〇.整体架构 一.装配Bean 1 <bean id="Foo" class="com.will.Foo"/> <!--使用id命名,不可id相同--> 2 <bean name="#Foo" class="com.will.Foo"/> <!--使用name命名,没有规范限制,而且可以重复,返回后面一个--> 3 <bean class="com.will.Fo

Spring IOC容器中注入bean

一.基于schema格式的注入 1.基本的注入方式 (属性注入方式) 根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求 <bean id="student" class="com.lq.ioc.Student"> <property name="name" value="zhansan"></property> <propert

Spring事务管理--[基于XML的配置]

我觉得自己写的不好,所以先贴一个写的好的帖子 感觉看完不用回来了.... 这是一个大佬写的的博客 : https://www.cnblogs.com/yixianyixian/p/8372832.html 第一:JavaEE 体系进行分层开发,事务处理位于业务层,Spring 提供了分层设计业务层的事务处理解决方 案. 第二:spring 框架为我们提供了一组事务控制的接口.具体在后面的第二小节介绍.这组接口是在 spring-tx-5.0.2.RELEASE.jar 中. 第三:spring

Spring中装配bean的三种主要方式

1.自动化配置 package com.springinaction.test; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration//告诉spring这是一个配置类 @ComponentScan//扫描组件 public class CDPlayerConfig { } pa

spring,springmvc,mybatis基本整合(一)--xml文件配置方式(2)

spring,springmvc,mybatis基本整合(一)–xml文件配置方式(2)之mapper接口 一,整合结构 二,所需jar包 如上图. 三,整合配置 1,web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://j

Spring IOC容器-自动装配

1 autowire="byName" 根据名称自动装配,自动去IOC容器中找与属性名同名的引用的对象,并自动注入. <!-- ###############自动装配############### --> <bean id="userDao" class="d_auto.UserDao"></bean> <bean id="userService" class="d_auto