您的位置:首页 > 编程语言 > Java开发

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

2018-03-01 19:45 676 查看
spring的两种依赖注入方式:setter注入与构造方法注入,这两种方法的不同主要就是在xml文件下对应使用property和constructor-arg属性, 例如:
property属性:<property name="id" value="123"></property>(其中name的值为原类中的属性名)

constructor-arg属性:<constructor-arg index="0" value="456"></constructor-arg>(其中index的值为0~n-1,n代表构造函数中的输入参数的数量)
1.setter方法注入setter方法注入即是创建一个普通的JavaBean类,为需要注入的属性通过对应的setter方法即可,如:(1)创建一个Id类:
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"> 
<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());
System.out.println(id.getName());
}
}
运行结果如下:2.构造方法注入(1)将上面的Id.class修改为:
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,运行结果如下: 
456 dawang我们知道,Spring对象属性的注入方式有两种:设值注入和构造注入。先看代码: 
  假设有个类为People,该对象包含三个属性,name和school还有age,这些属性都有各自的setter和getter方法,还有一个包含这三个属性的构造方法。如果用spring来管理这个对象,那么有以下两种方式为People设置属性: 
  1.设值注入:
<bean id="people" class="com.abc.People">
   <property name="name" value="张三" /> <!-- 设值注入 -->
   <property name="school" ref="school" /> <!-- 设值注入 -->
   <property name="age" value="20" type="int" />
</bean>
<bean id="school" class="com.abc.School" />
  2.构造注入:
<bean id="people" class="com.abc.People">
<!-- 构造注入,index=0表示构造器的第一个参数 -->
   <constructor-arg index="0" value="张三"/>
   <constructor-arg index="1" ref="school" /> <!-- 构造注入 -->
   <constructor-arg index="2" value="20" type="int" />
</bean>
<bean id="school" class="com.abc.School" />
  那么,这两种注入方式有和区别呢?下面做简单比较: 
  在过去的开发过程中,这两种注入方式都是非常常用的。Spring也同时支持两种依赖注入方式:设值注入和构造注入。 这两种依赖注入的方式,并没有绝对的好坏,只是适应的场景有所不同。相比之下,设值注入有如下优点:设值注入需要该Bean包含这些属性的setter方法
与传统的JavaBean的写法更相似,程序开发人员更容易理解、接收。通过setter方法设定依赖关系显得更加只管。
对于复杂的依赖关系,如果采用构造注入,会导致构造器国语臃肿,难以阅读。Spring在创建Bean实例时,需要同时实例化器依赖的全部实例,因而导致性能下降。而使用设值注入,则能避免这些问题
尤其是在某些属性可选的情况况下,多参数的构造器显得更加笨重
  构造注入也不是绝对不如设值注入,在某些特定的场景下,构造注入比设值注入更加优秀。构造注入有以下优势:构造注入需要该Bean包含带有这些属性的构造器
构造注入可以在构造器中决定依赖关系的注入顺序,优先依赖的优先注入。例如,组件中其他依赖关系的注入,常常要依赖于DataSrouce的注入。采用构造注入,可以在代码中清晰的决定注入顺序。
对于依赖关系无需变化的Bean,构造注入更有用处。因为没有Setter方法,所有的依赖关系全部在构造器内设定。因此,无需担心后续的代码对依赖关系产生破坏。
依赖关系只能在构造器中设定,则只有组件的创建者才能改变组件的依赖关系。对组件的调用者而言,组件内部的依赖关系完全透明,更符合高内聚的原则。
  建议:采用以设值注入为主,构造注入为辅的注入策略。对于依赖关系无需变化的注入,尽量采用构造注入;而其他的依赖关系的注入,则考虑采用设值注入。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: