Spring-Ioc-DI的三种注入方式:

一、Ioc与DI分别指什么?

Ioc控制反转:依赖spring对象工厂完成对象的创建

DI依赖注入:在Spring完成对象创建的同时依赖Spring容器完成对象属性的赋值

1.1 Ioc:

当我们需要通过Spring对象创建某个类的对象的时候,需要将这个交给Spring管理----通过<bean>标签

1.2 DI :

通过Spring容器给创建的对象属性赋值

1.3 Spring-Ioc-DI的三种方式:

Spring容器加载配置文件后,通过反射创建的对象,并给属性赋值。Spring容器通过反射实现属性的注入有以下三种方式:

  • 通过Setter()方法注入
  • 通过构造器注入
  • 通过接口注入

二. Spring-Ioc-DI Setter()注入 :

2.1 简单类型及字符串:

        在<bean>标签中配置<property>标签给对象标签赋值,实际上就是通过反射调用Setter()方法,简单类型及字符串类型,直接通过<property>标签中的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="student" class="com.xgsm.iop.entity.Student">
        <property name="stuNum" value="10001"></property>
        <property name="stuName" value="张飞"></property>
        <property name="stuAge" value="22"></property>
        <property name="stuGender" value="女"></property>
  
    </bean>
</beans>

2.2 日期类型(Date):

        Date类型有两种属性值注入方式;第一种:在<property>标签中通过ref属性引用Spring容器中的一个对象

<?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="date" class="java.util.Date"></bean>
    <bean id="student" class="com.xgsm.iop.entity.Student">
        <property name="stuNum" value="10001"></property>
        <property name="stuName" value="张飞"></property>
        <property name="stuAge" value="22"></property>
        <property name="stuGender" value="女"></property>
        <property name="enterenceTime" ref="date"></property>
    </bean>
</beans>

第二种方式:则是通过<property>标签中配置<bean>标签给Date类型的enterenceTime(入学日期)赋值;

<?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="student" class="com.xgsm.iop.entity.Student">
        <property name="stuNum" value="10001"></property>
        <property name="stuName" value="张飞"></property>
        <property name="stuAge" value="22"></property>
        <property name="stuGender" value="女"></property>
        <property name="enterenceTime">
            <bean class="java.util.Date"/>
        </property>
    
    </bean>
</beans>

2.3 自定义对象类型:

  • 方式一:通过<property >标签的ref属性方法进行引入
  • 方式二:在<property>双标签中添加<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="classRoom" class="com.xgsm.iop.entity.ClassRoom"></bean>

    <bean id="student" class="com.xgsm.iop.entity.Student">
        <property name="stuNum" value="10001"></property>
        <property name="stuName" value="张飞"></property>
        <property name="stuAge" value="22"></property>
        <property name="stuGender" value="女"></property>
        <property name="enterenceTime">
            <bean class="java.util.Date"/>
        </property>
        <!--自定义类型  方式一-->
        <!--<property name="classRoom" ref="classRoom"></property>-->
        <!--自定义类型  方式二-->
        <property name="classRoom">
            <bean class="com.xgsm.iop.entity.ClassRoom"></bean>
        </property>
    </bean>
</beans>

2.4 集合类型List、Set、Map

        在Student实体类中添加List<String> hobbies; Set<String> sets;  Map<String,Object> mapps集合属性,生成对应的Setter()方法,并重写ToString()方法

2.4.1 List<>集合

         类型一:如果List<简单类型或字符串> ,直接通过<property>标签中的value进行赋值;

 <!--List<简单类型或字符串> -->
        <property name="hobbies" value="旅游,电影"></property>

        类型二:如果List<对象>,List元素是对象类型,需要通过子标签<List>进行赋值

<!--List<对象类型> -->
        <property name="hobbies">
            <list>
                <bean class="com.xgsm.iop.entity.Book"></bean>
                <bean class="com.xgsm.iop.entity.Book"></bean>
            </list>
        </property>

         类型二中的<bean>也可以使用<ref bean = " "> 引用定义好的Book类型;

<?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="book" class="com.xgsm.iop.entity.Book"></bean>
    
        <!-- <!-List<对象类型> -->
        <property name="hobbies">
            <list>
                <!--<bean class="com.xgsm.iop.entity.Book"></bean>-->
                <!--<bean class="com.xgsm.iop.entity.Book"></bean>-->
                <ref bean="book"></ref>
                <ref bean="book"></ref>
            </list>
        </property>
    </bean>
</beans>

2.4.2  Set<>集合:

        类型一:set<简单类型或字符串>,直接通过<property>标签中的value进行赋值;

 <property name="sets" value="set1,set2"></property>

        类型二: 如果set<对象>,set元素是对象类型,需要通过子标签<set>进行赋值;

 <!--set<对象>类型-->
        <property name="sets">
            <set>
                <bean class="com.xgsm.iop.entity.ClassRoom"></bean>
                <bean class="com.xgsm.iop.entity.ClassRoom"></bean>
            </set>
        </property>

           类型二中的<bean>也可以使用<ref bean = " "> 引用定义好的ClassRoom类型;

<?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="classRoom" class="com.xgsm.iop.entity.ClassRoom"></bean>
   
        <!--set<对象>类型-->
        <property name="sets">
            <set>
              <ref bean="classRoom"></ref>
                <ref bean="classRoom"></ref>
            </set>
        </property>
    </bean>
</beans>

小结:Set<>和List<>集合的注入方式相同使用方式基本一致,唯一不同的就是将<List>标签改成<set>,反之亦然~

2.4.3 Map类型--键值对 :

        Map<String,Object>类型是键值对类型的,在为对象赋值的时候需要在<Property>标签中使用<map>子标签,而子标签中需要使用<entry>标签,在<entry>标签中分别有<key>、<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">

        <!--Map集合-->
        <property name="mapps">
            <map>
                <entry>
                    <key>
                        <value>key1</value>
                    </key>
                    <value>k1</value>
                </entry>
            </map>
        </property>
    </bean>
</beans>

三. Spring-Ioc-DI 构造器注入 :

3.1 简单类型及字符串:

        常规类型包括了简单类型、字符串、对象三大部分,<bean>标签其实就是调用对象的无参构造方法的过程,所以如果没有无参构造器,在<bean>配置时不赋值会报错;赋值时要和构造方法的属性顺序保持一致,也可以使用index索引(本示例中使用的是index索引)

<?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="classRoom" class="com.xgsm.iop.entity.ClassRoom"></bean>
    <bean id="book" class="com.xgsm.iop.entity.Book"></bean>
    <bean id="date" class="java.util.Date"></bean>

    <bean id="student" class="com.xgsm.iop.entity.Student">
        <constructor-arg value="10001" index="0"></constructor-arg>
        <constructor-arg value="zhangfei" index="1"></constructor-arg>
        <constructor-arg value="女" index="2"></constructor-arg>
        <constructor-arg ref="date" index="4"></constructor-arg>
        <constructor-arg value="3" index="3"></constructor-arg>
        <constructor-arg ref="classRoom" index="5"></constructor-arg>
    </bean>
</beans>

        如果在给自定义对象类型赋值时,不使用ref属性可以在<constructor-arg >标签中添加<bean>子标签;

<constructor-arg index="5">
            <bean class="com.xgsm.iop.entity.ClassRoom"></bean>
        </constructor-arg>

3.2 集合类型List、Set、Map:

        集合类型List、Set、Map构造器注入方式与Setter()的注入方式,有异曲同工之妙,注入方式大差不差,具体如下;

<?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="classRoom" class="com.xgsm.iop.entity.ClassRoom"></bean>
    <bean id="book" class="com.xgsm.iop.entity.Book"></bean>
    <bean id="date" class="java.util.Date"></bean>
    <bean id="student" class="com.xgsm.iop.entity.Student">
        <constructor-arg value="10001" index="0"></constructor-arg>
        <constructor-arg value="zhangfei" index="1"></constructor-arg>
        <constructor-arg value="女" index="2"></constructor-arg>
        <constructor-arg ref="date" index="4"></constructor-arg>
        <constructor-arg value="3" index="3"></constructor-arg>
        <constructor-arg index="5">
            <bean class="com.xgsm.iop.entity.ClassRoom"></bean>
        </constructor-arg>


        <!-- 集合类型List、Set、Map:-->

        <!--List<>集合类型-->
        <constructor-arg index="6">
            <list>
                <value>11</value>
                <value>22</value>
            </list>
        </constructor-arg>

        <!--Set<>集合类型:-->
        <constructor-arg index="7">
            <set>
                <value>33</value>
                <value>44</value>
            </set>
        </constructor-arg>


        <!--map<String,Object>键值对-->
        <constructor-arg index="8">
            <map>
                <entry>
                    <key><value>key3</value></key>
                    <value>k33</value>
                </entry>
                <entry>
                    <key><value>key4</value></key>
                    <value>k44</value>
                </entry>
            </map>
        </constructor-arg>
    </bean>
</beans>

          第三种注入方式是接口,但是在我们日常使用中基本上不使用,而以上两种使用的较为广泛,所以在此进行了详细的描述,希望有助于大家,Thanks♪(・ω・)ノ浏览~~~~

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暇光曙墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值