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

Spring之DI简述

2016-05-02 11:39 519 查看
Spring的依赖注入原理,我的理解为:在xml配置文件中,“构造”对象。用引号的原因在于,在这里并不是真正的构造。只有当我们通过ApplicationContext来加载xxx.xml文件时,这些类的对象才会被真正构造。

然而,如何构造呢?简单的来说,Spring通过调用类的构造函数进行构造。构造函数大体分两种:默认构造函数与带参数的构造函数。也就是说,Spring中通过依赖注入来构建对象的最终方法依旧是通过:类的构造函数来完成,与我们手工通过new来构造对象,并没有什么不同。

Spring的DI大体分为两种:显示构造函数DI与setter方法DI。等等,不是说,都要用构造函数(反射除外)来构造对象吗?setter方法也可以构造对象?

首先,setter方法不能构造对象,它只能修改对象的属性而已。那为什么Spring存在setter方法DI呢?其实,当采用setter方法DI时,Spring先调用的类的默认构造函数,先生成一个对象,然后调用该对象的setter方法,来修改对象的属性值。所以,采用setter方法DI时,该类必须拥有默认构造函数,否则,会构造失败。

来通过代码具体说明。

先建立一个接口:

package com.bean.DI2;

public interface People {
public void say();
}
在定义两个实现类:

package com.bean.DI2;

public class Male implements People {

private int age;
private int height;
private String name;
public Male() {
this.age=18;
this.height=170;
this.name="Bob";
}
public Male(int age,int height,String name){
this.age=age;
this.height=height;
this.name=name;
}

public void setAge(int age) {
this.age = age;
}
public void setHeight(int height) {
this.height = height;
}
public void setName(String name) {
this.name = name;
}
@Override
public void say() {
System.out.println("I'm male"+",name is:"+this.name+
",age is:"+this.age+
",height is:"+this.height);
}

}
package com.bean.DI2;

public class Female implements People {

private int age;
private int height;
private String name;
public Female(){
this.age=0;
this.height=0;
this.name="null";
System.out.println("default constructor:"+
",age is:"+this.age+
",height is:"+this.height+
",name is:"+this.name);
}

public Female(int age,int height,String name){
this.age=age;
this.height=height;
this.name=name;
}

public void setAge(int age) {
this.age = age;
}

public void setHeight(int height) {
this.height = height;
}

public void setName(String name) {
this.name = name;
}

@Override
public void say() {
System.out.println("I'm female"+",name is:"+this.name+
",age is:"+this.age+
",height is:"+this.height);
}

}

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"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<!-- 调用默认构造函数 -->
<bean id="male" class="com.bean.DI2.Male">
</bean>

<!-- 构造函数注入 -->
<bean id="male2" class="com.bean.DI2.Male" c:age="22" c:height="180" c:name="BigBob">
</bean>

<!-- setter注入 -->
<bean id="male3" class="com.bean.DI2.Male" p:age="35" p:height="177" p:name="OldBob"></bean>

<!-- setter注入,必须拥有默认构造函数 -->
<!-- setter原理为,先调用默认构造函数生成一个对象,然后在调用set方法,修改对象的属性 -->
<bean id="female" class="com.bean.DI2.Female" p:age="16" p:height="160" p:name="rose"></bean>
</beans>


测试主函数如下:

package com.bean.DI2;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {
public static void main(String[] args) {
FileSystemXmlApplicationContext context=new FileSystemXmlApplicationContext("BeanXml\\DI2.xml");
Male male=(Male)context.getBean("male");
male.say();
Male male2=(Male)context.getBean("male2");
male2.say();
Male male3=(Male)context.getBean("male3");
male3.say();
Female female=(Female)context.getBean("female");
female.say();
context.close();
}
}

测试结果如下:



通过上述结果可以看出,setter方法DI,就是先调用了类的默认构造函数,然后在调用setter方法来修改该对象的属性。

在一个bean.xml文件中,一个bean的id值,相当于该对象的名字,即引用。所以,在一个bean.xml文件中,id不能重复。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: