前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring 学习笔记(四)—— XML配置依赖注入

Spring 学习笔记(四)—— XML配置依赖注入

作者头像
Rekent
发布2018-09-04 15:22:38
3550
发布2018-09-04 15:22:38
举报
文章被收录于专栏:日常分享日常分享日常分享

  依赖注入(DI)与控制反转(IoC)是同一个概念,都是为了处理对象间的依赖关系。

  通过DI/IoC容器,相互依赖的对象由容器负责创建和装配,而不是在代码中完成。

  Spring支持通过setter方法和构造方法两种方式完成注入。


  Setter方法注入

    setter方法注入是最常见的一种注入方式。Spring先调用Bean的默认构造函数实例化Bean对象,然后通过反射的方式调用setter方法注入属性值。

    与之前的示例相同,可查看前面的笔记。


  构造方法注入

    如果Bean的属性中有一些是必须赋值的,或者对多个属性的赋值顺序有要求,则使用setter方法注入可能会造成错误。

    只能通过人为保证,而使用构造方法注入可以保证IoC容器提供的Bean实例一定是可用的。

  示例如下:

public class Order {
    public String OrderNum;
    public int id;
    public double price;

    public Order(String NO,int id,double price){
        this.OrderNum=NO;
        this.id=id;
        this.price=price;
    }


}
<?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="Order" class="Demo03.Order">
            <constructor-arg name="id" value="100"/>
            <constructor-arg name="NO" value="200"/>
            <constructor-arg name="price" value="5.7"/>
        </bean>
</beans>

两种注入方式的区别

  对于复杂的依赖关系,如果采用构造方式注入,会导致构造器过于臃肿,难以阅读。尤其是在某些属性可选的情况下,多参数的构造器更加笨重。

  构造方法注入可以在构造器中确定依赖关系的注入顺序,当某些属性的赋值操作是由先后顺序时,这点尤为重要。

  对于依赖关系无须变化的Bean,构造注入更有用处。


注入值类型

  针对注入的值,Spring支持三种类型:字面值、其他Bean的引用、集合类型。

  集合支持数组(array)、List(list)、Set(set)、Map(map)、Properties(props)。

  示例:

<?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="Demo02.Book">
        <property name="name" value="Richard"/>
        <property name="author" value="Rekent"/>
        <property name="publisher" value="Cheung"/>
        <property name="price" value="70"/>
        <property name="order" ref="Order"/>
    </bean>

    <bean id="Order" class="Demo03.Order">
        <constructor-arg name="id" value="100"/>
        <constructor-arg name="NO" value="200"/>
        <constructor-arg name="price" value="5.7"/>
    </bean>

    <bean id="CollectionDemo" class="Demo04.CollectionDemo">
        <property name="list">
            <list>
                <value>1</value>
                <value>2</value>
            </list>
        </property>

        <property name="set">
            <set>
                <value>1</value>
                <value>2</value>
            </set>
        </property>

        <property name="map">
            <map>
                <entry key="1" value="2"/>
                <entry key="2" value-ref="book"/>
            </map>
        </property>

        <property name="properties">
            <props>
                <prop key="1">22</prop>
                <prop key="2">BB</prop>
            </props>
        </property>
    </bean>
</beans>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  •   Setter方法注入
  •   构造方法注入
  • 两种注入方式的区别
  • 注入值类型
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档