spring中基于XML的依赖注入配置

依赖注入:Dependency Injection
       IOC的作用:降低程序间的耦合(依赖关系)
       依赖关系的管理:交给spring来维护
       在当前类需要用到其他类的对象,由spring提供,我们只需要在配置文件中说明。
       依赖关系的维护就称之为依赖注入。

依赖注入的方式

第一种:使用构造函数注入

User实体类中的属性为: String userName; int age;Date date;没有提供默认构造函数

使用的标签:constructor-arg
标签出现的位置:bean标签的内部
标签中的属性
       type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型
        index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始
        name:用于指定给构造函数中指定名称的参数赋值
----------------------以上三个用于指定给构造函数中哪个参数赋值------------------------------
       value:用于提供基本类型和String类型的数据
       ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象

优势:
       在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
弊端:
       改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。

<?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="user" class="cn.td.domain.User" scope="singleton">
        <constructor-arg name="userName" value="张三"/>
        <constructor-arg name="age" value="18"/>
        <constructor-arg name="date" ref="data"/>
    </bean>
    <bean id="data" class="java.util.Date"/>
</beans>

第二种:使用set方法注入

set方法注入更常用
涉及的标签:property
出现的位置:bean标签的内部
标签的属性
       name:用于指定注入时所调用的set方法名称
       value:用于提供基本类型和String类型的数据
       ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象
优势:
       创建对象时没有明确的限制,可以直接使用默认构造函数
弊端:
       如果有某个成员必须有值,则获取对象是有可能set方法没有执行。

<?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="data" class="java.util.Date"/>

    <!--set注入-->
    <bean id="user" class="cn.td.domain.User" scope="singleton">
        <property name="userName" value="张三"/>
        <property name="age" value="18"/>
        <property name="date" ref="data"/>
    </bean>
</beans>

依赖注入的数据类型

基本类型和String

User实体类中的属性为: String userName; int age;

<bean id="user" class="cn.td.domain.User" scope="singleton">
    <constructor-arg name="userName" value="张三"/>
    <constructor-arg name="age" value="18"/>
</bean>

其他bean类型(在配置文件中或者注解配置过的bean)

User实体类中的属性为: String userName; int age;Date date;
其中,Date类型不是基本类型和String类型此时,应在配置文件中配置一个日期对象,然后再用ref引入

<bean id="user" class="cn.td.domain.User" scope="singleton">
   <constructor-arg name="userName" value="张三"/>
    <constructor-arg name="age" value="18"/>
    <constructor-arg name="date" ref="data"/>
</bean>
<bean id="data" class="java.util.Date"/>

复杂类型/集合类型

复杂类型/集合类型注入时,结构相同,标签可以互换

User中的属性
String[] strings; List list; Set set; Map<String,String> map; Properties prop;

用于给List结构集合注入的标签: array list set
用于给数组注入

<!--数组注入-->
<bean id="user" class="cn.td.domain.User">
    <property name="strings">
        <array>
            <value>aaa</value>
            <value>bbb</value>
            <value>ccc</value>
        </array>
    </property>
</bean>

用于给List注入

<bean id="user" class="cn.td.domain.User">
    <property name="list">
        <list>
            <value>listaaa</value>
            <value>listbbb</value>
            <value>listccc</value>
        </list>
    </property>
</bean>

用于给set注入

<bean id="user" class="cn.td.domain.User">
    <property name="set">
        <list>
            <value>setaaa</value>
            <value>setbbb</value>
            <value>setccc</value>
        </list>
    </property>
</bean>

用于个Map结构集合注入的标签: map props
用于给map注入

<bean id="user" class="cn.td.domain.User">
    <property name="map">
        <map>
            <entry key="mapA" value="mapa"/>
            <entry key="mapB" value="mapb"/>
            <entry key="mapC" value="mapc"/>
        </map>
    </property>
</bean>

用于给 props注入

<bean id="user" class="cn.td.domain.User">
    <property name="prop">
        <props>
            <prop key="propA">propa</prop>
            <prop key="propB">propb</prop>
            <prop key="propC">propc</prop>
        </props>
    </property>
</bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值