XML 指可扩展标记语言(eXtensible Markup Language)。
XML 被设计用来传输和存储数据。
XML最适合作为数据交换的标准,这也是 Spring极力推荐使用XML文件存放配置信息的原因。
在xml配置文件的开头通常会指定版本和编码方式,加入:<?xml version="1.0" encoding="UTF-8"?>
1.beans标记
整个配置文件的根节点,包含一个或多个Bean元素。在该标记中可配置命名空间与schema的装载路径,还可以通过default-init-method属性为该配置文件中的所有Bean实例统一指定初始化方法,通过default-destroy-method属性为该配置文件中的所有Bean实例统一指定销毁方法,通过default-lazy-init属性为该配置文件中的所有Bean实例统一指定是否进行迟延加载
实例:
<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:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
</beans>
2.bean标记
使用该标记定义一个Bean的实例化信息,用以指导Bean工厂正确地进行Bean的生产与装配。由class属性指定类全名,由id(推荐使用)或name属性指定生成的Bean实例名称。init-method属性指定初始化时要调用的方法,destroy-method属性实例销毁时要调用的方法。Bean实例的依赖关系可通过property子标记(set方式注入)或constructor-arg子标记(构造方式注入)来定义。bean标记中的scope属性用以设定Bean实例的生成方式,当scope设置为singleton或默认时以单例模式生成,当scope设置为prototype时则以原型(多例)模式生成。
实例:
bean标记需要作为beans的子标记
<bean id="autoLogBean" class="com.deron.publics.aop.AutoLogBean"/>
3.property标记
它是bean标记的子标记,用以调用Bean实例中的相关Set方法完成属性值的赋值,从而完成依赖关系的注入。name属性指定Bean实例中的相应属性名称,属性值可通过ref属性或value属性直接指定,也可以通过ref或value子标记指定
实例:
<bean id="autoLogBean" class="com.deron.publics.aop.AutoLogBean">
<property name="propertyBean" ref="propertyBean" />
</bean>
<bean id="propertyBean" class="com.deron.publics.bean.PropertyBean"/>
上面的例子是AutoLogBean这个类一个有一个setPropertyBean的方法,实例化AutoLogBean的时候通过这个方法指定AutoLogBean的propertyBean属性的值,通过ref属性指定。
4.ref标记
该标记通常作为constructor-arg、property、list、set、entry等标记的子标记,由bean属性指定一
个Bean工厂中某个Bean实例的引用。
ref引用其他的bean实例,所以假如指定<ref bean="testBean"/>,在xml中一定要有另外一个bean,它的id为"testBean"。
实例:
<bean id="PrinterHelper" class="com.java2s.output.PrinterHelper">
<property name="outputGenerator" >
<ref bean="CSVPrinter"/>
</property>
</bean>
<bean id="CSVPrinter" class="com.java2s.output.CSVPrinter">
其实这个ref标记跟ref属性的作用一样,常用的做法是不用ref标记而使用ref属性。
4.constructor-arg标记
它是bean标记的子标记,用以传入构造参数进行实例化,这也是注入依赖关系的一种常见方式。index属性指定构造参数的序号(从0开始),当只有一个构造参数是通常省略不写;type属性指定构造参数的类型,当为基本数据类型时亦可省略此属性;参数值可通过ref属性或value属性直接指定,也可以通过ref或value子标记指定
5.value标记
该标记通常作为constructor-arg、property、list、set、entry等标记的子标记,用以直接指定一个常
量值。
实例:
<bean id="datasource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="resourceRef">
<value>false</value>
</property>
<property name="jndiName">
<value>java:comp/env/jdbc/FtnDormNew</value>
</property>
</bean>
6.prop标记
该标记通常用做props标记的子标记,用以设置一个“键/值”对。key属性接收字符串类型的“键”名称,“值”则可放置在prop标记体内。
7.entry标记
该标记通常用做map标记的子标记,用以设置一个“键/值”对。key属性接收字符串类型的“键”名称,“值”则可由ref或value子标记指定。