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注入):
- 设值注入需要该Bean包含这些属性的setter方法
- 与传统的JavaBean的写法更相似,程序开发人员更容易理解、接收。通过setter方法设定依赖关系显得更加只管。
- 对于复杂的依赖关系,如果采用构造注入,会导致构造器国语臃肿,难以阅读。Spring在创建Bean实例时,需要同时实例化器依赖的全部实例,因而导致性能下降。而使用设值注入,则能避免这些问题
- 尤其是在某些属性可选的情况况下,多参数的构造器显得更加笨重
构造注入:
- 构造注入需要该Bean包含带有这些属性的构造器
- 构造注入可以在构造器中决定依赖关系的注入顺序,优先依赖的优先注入。例如,组件中其他依赖关系的注入,常常要依赖于DataSrouce的注入。采用构造注入,可以在代码中清晰的决定注入顺序。
- 对于依赖关系无需变化的Bean,构造注入更有用处。因为没有Setter方法,所有的依赖关系全部在构造器内设定。因此,无需担心后续的代码对依赖关系产生破坏。
- 依赖关系只能在构造器中设定,则只有组件的创建者才能改变组件的依赖关系。对组件的调用者而言,组件内部的依赖关系完全透明,更符合高内聚的原则。