一、简介

Spring依赖注入有两种方式一种是基于注解的方式一种是基于xml的方式。这里主要介绍xml如何配置。

二、spring beans.xml基本配置

beans.xml可以在官方文档中找到,我这里摘抄了下来,下面的xml就是Spring最基本的beans.xml的一个配置

<?xml version="1.0" encoding="UTF-8"?>
<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="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>
三、基于xml方式的依赖注入配置

spring的注入方式有三种,一种是构造器注入,一种是set注入,还有一种是基于注解来注入。这里只介绍构造器注入和set注入(本文只介绍xml中常见的几种类型的注入)

  • 构造器注入
    构造器注入使用的标签是<constructor-arg> name是构造器中属性的名称,value是对应的值
<?xml version="1.0" encoding="UTF-8"?>
<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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
   <bean id = "t1" class="com.haici.spring.demo.entity.Teacher">
        <constructor-arg name="name" value="王老师"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg name="phone" value="123456789"/>
        <constructor-arg name="sex" value="女"/>
    </bean>
</beans>
  • set注入
    所谓set注入就是指通过<property>标签来给对象的属性进行设值,底层是通过java的反射机制来实现的,set注入要求对象必须有set方法。没有set方法属性是注入不了的,set注入又分以下几种情况
  • 属性是java基本类型或者String类型时
    属性是java基本类型或者String类型时,使用value来为字段设值
<bean id = "student" class="com.haici.spring.demo.entity.Student">
    <property name="name" value="张三"/>
</bean>
  • 属性是java对象类型的时候
    属性是java对象类型时采用ref标签来注入
<?xml version="1.0" encoding="UTF-8"?>
<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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id = "t1" class="com.haici.spring.demo.entity.Teacher">
    	<constructor-arg name="name" value="王老师"/>
    	<constructor-arg name="age" value="23"/>
    	<constructor-arg name="phone" value="123456789"/>
    	<constructor-arg name="sex" value="女"/>
	</bean>
	<bean id = "student" class="com.haici.spring.demo.entity.Student">
		<property name="teachers" ref="t1">
	</bean>
</beans>
  • 属性是数组的时候
    属性是数组的时候通过<array>标签来注入
<property name="teachers">
    <array>
     <ref bean="t1"/>
     <ref bean="t2"/>
    </array>
</property>
  • 属性是List的时候
    属性是List的时候通过<list>标签来注入
<property name="courses">
   <list>
       <value>语文</value>
       <value>数学</value>
       <value>外语</value>
   </list>
</property>
  • 属性是set的时候
    属性是set的时候通过<set>标签来注入
<property name="hobbis">
   <set>
       <value>打篮球</value>
       <value>看电影</value>
   </set>
</property>
  • 属性是map的时候
    属性是set的时候通过<map>标签来注入
<property name="scoreMap">
       <map>
           <entry key="语文" value="79.5"/>
           <entry key="数学" value="85"/>
           <entry key="外语" value="88"/>
       </map>
   </property>
  • 属性是properties的时候
    属性是properties的时候通过<props>标签来注入
<property name="config">
   <props>
       <prop key="username">张三</prop>
   </props>
</property>
  • util命名空间的使用
    有这样一种场,如Student对象包含一个List<String>属性(course),对于同一个班的学生来说,course都应该是一样的,那么可以抽取出来作为公用属性,这样在配置Student的bean的时候 就不需要每次都用<List>一个一个的重复设值了。这个时候可以用util命名空间的方式来满足需求
    (一)导入util命名空间
    导入util命名空间在bean.xml中天健utl的命名空间的地址以及对应的xsd
xmlns:util="http://www.springframework.org/schema/util
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd
  • 此时bean.xml文件头变成
<beans xmlns="http://www.springframework.org/schema/beans"
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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd">
</beans>
  • (二)配置及应用公共的集合类
<!-- 使用util命名空间创建公用的集合类型对象 这样别的类去引用的时候可以通过ref来引用公用的集合-->
<util:map id="m1">
    <entry key="语文" value="79.5"/>
    <entry key="数学" value="85"/>
    <entry key="外语" value="88"/>
</util:map>

<bean id = "student2" class="com.haici.spring.demo.entity.Student">
    <property name="name" value="张三"/>
    <property name="age" value="18"/>
    <property name="sex" value="男"/>
    <property name="courses">
        <list>
            <value>语文</value>
            <value>数学</value>
            <value>外语</value>
        </list>
    </property>
    <!--注入set属性使用set标签 -->
    <property name="hobbis">
        <set>
            <value>打篮球</value>
            <value>看电影</value>
        </set>
    </property>
    <!-- 注入map属性使用map标签-->
    <property name="scoreMap" ref="m1">
        <!--<map>
            <entry key="语文" value="79.5"/>
            <entry key="数学" value="85"/>
            <entry key="外语" value="88"/>
        </map>-->
    </property>
    <!--注入数组 使用array标签-->
    <property name="teachers">
        <array>
            <ref bean="t1"/>
        </array>
    </property>
</bean>
  • parent属性的使用
    <bean>标签有一个parent属性,可以用来继承某个bean的属性,这样可以避免重复的配置(注意这里和java的继承不一样,同一个类的不同对象的属性也可以被继承 相当于属性的拷贝)
<!-- 使用util命名空间创建公用的集合类型对象 这样别的类去引用的时候可以通过ref来引用公用的集合-->
<util:map id="m1">
    <entry key="语文" value="79.5"/>
    <entry key="数学" value="85"/>
    <entry key="外语" value="88"/>
</util:map>

<bean id = "student2" class="com.haici.spring.demo.entity.Student">
    <property name="name" value="张三"/>
    <property name="age" value="18"/>
    <property name="sex" value="男"/>
    <property name="courses">
        <list>
            <value>语文</value>
            <value>数学</value>
            <value>外语</value>
        </list>
    </property>
    <!--注入set属性使用set标签 -->
    <property name="hobbis">
        <set>
            <value>打篮球</value>
            <value>看电影</value>
        </set>
    </property>
    <!-- 注入map属性使用map标签-->
    <property name="scoreMap" ref="m1">
        <!--<map>
            <entry key="语文" value="79.5"/>
            <entry key="数学" value="85"/>
            <entry key="外语" value="88"/>
        </map>-->
    </property>
    <!--注入数组 使用array标签-->
    <property name="teachers">
        <array>
            <ref bean="t1"/>
        </array>
    </property>
</bean>
<!-- 通过parent属性可以继承某个bean的属性 并不一定是java父子类的关系 相同的类也可以通过paretn来继承属性-->
<bean id = "student3" class="com.haici.spring.demo.entity.Student" parent="student2"/>

以上就是spring 基于xml方式的属性注入的常用配置