在Spring中常常使用 XML 文件来注册管理Bean对象的依赖关系,它是通过先定义,然后初始化和依赖注入的。Spring在启动时,就会查找程序中的XML文件,读取其中配置信息,根据配置信息完成一系列的操作。
Spring中的XML配置通常会根据内容起不同的名字,比如:spring-common.xml:用来存放通用的配置、spring-mybatis.xml:用来存放数据库相关的配置、spring-shiro.xml:用来存放shiro相关的配置。其中applicationContext.xml是Spring的默认配置文件,当容器启动时找不到指定的配置文档时,将会尝试加载这个默认的配置文件。
使用 beans 作为根标签(pom.xml使用project作为根标签,web.xml使用web-app作为根标签)
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
beans>
在 XML 文件中通过标签 < bean> < /bean> 定义一个Bean对象。
<bean id="" name="" class="" scope="" init-method="" destroy-method="">
bean>
Bean 属性注入,就是将属性注入到 Bean 中的过程,既可以注入普通属性,也可以注入一个对象(Bean)。
Spring 主要通过以下 2 种方式实现属性注入:
通过构造函数注入属性
<constructor-arg name="" value="" index="" ref=""/>
setter方法 注入
<property name="" value="" ref=""/>
<bean id="" class="com.******" scope="" init-method="" destroy-method="">
<constructor-arg name="属性名" value="值"/>
<constructor-arg index="" ref="引用对象"/> index是构造参数的顺序,从0开始,
<property name="属性名" value="值"/>
<property name="属性名" ref="注入一个bean对象,这里写id值"/> 引用对象
bean>
两个类:
然后通过XML配置对象

如果赋null值,不能直接 < property name=“” value=“null” /> 这样写,这样赋值赋的是null字符串,应该写成 :
< property name=“name”>
< null />
< /property>
在一个bean标签中再声明一个bean标签,这就是内部bean,假设一个bean中需要引用其他的对象时,之前我们是通过property、constructor-arg标签的 ref 属性来引入一个bean对象,而现在是直接在标签内创建bean标签:


除了内部bean外,我们还可以在 property、constructor-arg 便签内部中,配置 Java 集合类型的属性和参数(如 List、Set、Map、Properties 等等)。
| 标签 | 描述 |
|---|---|
| list | 注入一个list类型的值,允许重复 |
| array | 注入一个数据类型的值 |
| set | 注入set类型的值,不可重复 |
| map | 用于注入 key-value 的集合,其中 key 和 value 都可以是任意类型 |
| props | 用于注入 key-value 的集合,其中 key 和 value 都是字符串类型 |
| null | 注入空值 |
<bean id="" class="">
<property name="属性名">
<array>
<value>值value>
<ref bean="bean的id值"/> 注入的是bean对象
array>
property>
<property name="属性名">
<list>
<value>值value>
<ref bean="对象id值"/> 注入的是对象
list>
property>
<property name="属性名">
<set>
<value>值value>
<ref bean="对象id值"/> 注入的是对象
set>
property>
<property name="属性名">
<map>
<entry key="键" value="值"/>
<entry key-ref="" value-ref=""/>
map>
property>
<property name="属性名">
<props>
<prop key="键">值prop>
<prop key="键"/>
props>
property>
<property name="属性名">
<null/> 赋空值
property>
bean>


Spring使用XML进行属性装配其实并不是很复杂,不过XML里有命名空间这个概念,所以Spring也提供了 命名空间p和c 装配的方式。(p就是set方法注入,c就是构造方法注入)。除了这个外,还可以使用 util 注入集合等属性
命名空间装配可以减少标签的使用
如果你想使用命名空间p、c、和util,只需要在Spring 的XML 头文件中加入约束文件:
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person1" class="com.yu.pojo.Person" c:_0="" c:_1="" c:_2=""/>
<bean id="person2" class="com.yu.pojo.Person" p:name="" p:sex="" p:age=""/>
<util:list id="list">
<ref bean="p"/>
<value>Stringvalue>
util:list>
<util:set id="set">
<ref bean="p"/>
<value>Stringvalue>
util:set>
<util:map id="map">
<entry key-ref="p" value-ref="p"/>
util:map>
beans>


可以多配置一些xml文件,方便区分,然后用主文件引入这些xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="***.xml"/>
<import resource="***.xml"/>
.......
beans>
比如我们创建一个application.xml文件,然后在其中引入 beans.xml文件,引入后,我们可以通过获取application文件,就能获取到 beans 中定义的bean


Spring提供了自动装配的功能,在不使用 constructor-arg 和 property 手动装配的情况下也可以进行自动装配bean。(自动装配不能用在字面量的属性上,只能用在我们自定义的类)
Spring 的自动装配功能可以让 Spring 容器依据自动装配的规则(有五种),为指定的 Bean 从应用的上下文中查找它所依赖的 Bean,并自动建立 Bean 之间的依赖关系。
自动装配功能能够有效地简化 Spring 应用的 XML 配置,因此在配置数量多时采用自动装配可以降低工作量。Spring 框架式默认使不支持自动装配的,要想使用自动装配,则需要对 Spring XML 配置文件中 bean 元素的 autowire 属性进行设置。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="p1" class="****">bean>
<bean id="p2" class="****">bean>
<bean id="" class="">
<property name="p1" ref="p1"/>
<property name="p2" ref="p2"/>
bean>
<bean id="" class="" autowire="byName"/>
beans>

