Spring的两种依赖注入方式:setter方法注入与构造方法注入以及两种方式的区别!

spring的两种依赖注入方式:setter注入与构造方法注入.

这两种方法的不同主要就是在xml文件下对应使用property和constructor-arg属性,

如下图:

//其中name的值为原类中的属性名
property属性:<property name="id" value="123"></property>


//其中index的值为0~n-1,n代表构造函数中的输入参数的数量
constructor-arg属性:<constructor-arg index="0" value="456"></constructor-arg>

实例演示:

1.setter方法注入

setter方法注入即是创建一个普通的JavaBean类,为需要注入的属性通过对应的setter方法即可,如:

(1)创建一个Id类:只有set与get方法

    package com.loster.li;  

    public class Id {  

        private int id;  
        private String name; 
 
        public int getId() {  
            return id;  
        }  
        public void setId(int id) {  
            this.id = id;  
        }  
        public String getName() {  
            return name;  
        }  
        public void setName(String name) {  
            this.name = name;  
        }  
    }  

(2)创建配置文件Id_Bean.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  
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
        //settet方法注入
        <bean id="id" class="com.loster.li.Id">  
        <property name="id" value="123"></property>  
        <property name="name" value="xiaoli"></property>  
        </bean>  
    </beans> 
  •  

 (3)编写测试类IdTest.java,并运行:

 package com.loster.li;  

    import org.springframework.context.support.ClassPathXmlApplicationContext;  

    public class IdTest {  
        public static void main(String[] args){  
            ClassPathXmlApplicationContext context = new   
                    ClassPathXmlApplicationContext("com/loster/li/Id_Bean.xml");  

            Id id = (Id)context.getBean("id");  
            System.out.println(id.getId());   //123 
            System.out.println(id.getName());  //xiaoli
        }  
    }  

 2.构造方法注入

(1)给ID类创建有参构造方法:

  package com.loster.li;  

    public class Id {  
        private int id;  
        private String name;  
        //有参构造方法
        public Id(int id,String name){  
            this.id = id;  
            this.name = name;  
        }  
        public int getId() {  
            return id;  
        }  
        public void setId(int id) {  
            this.id = id;  
        }  
        public String getName() {  
            return name;  
        }  
        public void setName(String name) {  
            this.name = name;  
        }  
    }  

(2)将上面的Id_Bean.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  
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
        //构造方法注入
        <bean id="id" class="com.loster.li.Id">  
        <constructor-arg index="0" value="456"></constructor-arg>  
        <constructor-arg index="1" value="dawang"></constructor-arg>  
        </bean>  
    </beans>  

(3)再次运行IdTest.java,运行结果如下: 

    package com.loster.li;  

    import org.springframework.context.support.ClassPathXmlApplicationContext;  

    public class IdTest {  
        public static void main(String[] args){  
            ClassPathXmlApplicationContext context = new   
                    ClassPathXmlApplicationContext("com/loster/li/Id_Bean.xml");  

            Id id = (Id)context.getBean("id");  
            System.out.println(id.getId());   //123 
            System.out.println(id.getName());  //xiaoli
        }  
    }  

       那么,这两种注入方式有和区别呢?下面做简单比较: 
       设值注入(setter注入):

  1. 设值注入需要该Bean包含这些属性的setter方法
  2. 与传统的JavaBean的写法更相似,程序开发人员更容易理解、接收。通过setter方法设定依赖关系显得更加只管。
  3. 对于复杂的依赖关系,如果采用构造注入,会导致构造器国语臃肿,难以阅读。Spring在创建Bean实例时,需要同时实例化器依赖的全部实例,因而导致性能下降。而使用设值注入,则能避免这些问题
  4. 尤其是在某些属性可选的情况况下,多参数的构造器显得更加笨重

  构造注入:

  1. 构造注入需要该Bean包含带有这些属性的构造器
  2. 构造注入可以在构造器中决定依赖关系的注入顺序,优先依赖的优先注入。例如,组件中其他依赖关系的注入,常常要依赖于DataSrouce的注入。采用构造注入,可以在代码中清晰的决定注入顺序。
  3. 对于依赖关系无需变化的Bean,构造注入更有用处。因为没有Setter方法,所有的依赖关系全部在构造器内设定。因此,无需担心后续的代码对依赖关系产生破坏。
  4. 依赖关系只能在构造器中设定,则只有组件的创建者才能改变组件的依赖关系。对组件的调用者而言,组件内部的依赖关系完全透明,更符合高内聚的原则。

  建议:采用以设值注入为主,构造注入为辅的注入策略。对于依赖关系无需变化的注入,尽量采用构造注入;而其他的依赖关系的注入,则考虑采用设值注入。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值