Spring 的IOC 与DI 详解

一说到Spring我们就会想到它的一些框架和特性比如常用的AOP和IOC(DI),那下面我们变说说什么是IOC和DI。

IOC

所谓的IOC就是Inversion of Control 的缩写意即控制反转,

所谓反转是指应用本身并不负责依赖对象的创建和维护,而把这个任务交给第三方即Spring去处理,这是将创建对象的权利交给第三方,控制反转就是控制权的转移.

说白了就是对成员变量的赋值的控制权从代码中反转到配置文件中。

DI

所谓的DI就是Dependence Injection的缩写意即依赖注入.当IOC容器启动时,容器负责创建容器内的所有对象;根据配置信息形成对象之间的依赖关系.

说了很多直接上代码细细地嚼一下:

首先新建一个JAVA project,建立如下JAVA 文件,导入相应的jar包:

package com.importantNew;

public class HelloWorld {

	private String message;
	private String address;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	public void printMessage(){
		System.out.println("Your Message:Hi,"+message);
	}
	public void printAddress(){
		System.out.println("And here is your Location:"+address);
	}
}

在JAVA 文件中创建两个字段及其set方法和两个print方法(printMessage,和printAddress)用来模拟我们的业务逻辑Biz。

然后在classpath即src包下面新建一个bean.xml,或者如果按照职业习惯的话可以取名问applicationContext.xml的文件其配置如下:

<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="helloworld" class="com.importantNew.HelloWorld">
            <property name="message" value="Hill, Bill Gates"></property>
            <property name="address" value="WillStreet"></property>
    </bean>

</beans>

然后新建一个JAVA 文用于调用以上JAVA文件中我们创建的两个print方法用于模拟现实的Biz。

package com.importantNew;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

	public static void main(String[] args) {
                //创建spring 容器并加载bean
               ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
               //使用spring的依赖注入
                HelloWorld hello = (HelloWorld) context.getBean("helloworld");
		hello.printMessage();
		hello.printAddress();
	}
}

好了,用于介绍IOC和DI 的所有代码展示完毕了,接下来然我们仔细瞧瞧在以上代码中哪些体现了IOC,哪些体现了DI。

首先我们看DI的。

在MainApp.java中我们创建了spring 容器并加载了bean(即bean.xml),然后我们通过实例化HelloWorld来对其对象(既包含了message和address字段的对象)并为其对象赋值。

此段代码就能很好地体现spring的DI 特性:

                 //使用spring的依赖注入
                HelloWorld hello = (HelloWorld) context.getBean("helloworld");

通常情况下如果我们不适用spring的IOC或DI时,我们实例化一个对象的一般做法如下:

                HelloWorld hello1 = new HelloWorld();
然后对实例化的对象进行赋值和得值即set和get 方法。

                hello.setAddress("WillStreet");
		hello.setMessage("Hi,Bill Gates");
		System.out.println(hello.getAddress());
		System.out.println(hello.getMessage());


相信大家已经看到了spring DI的体现所在了,但我们实例化对象时的context.getBean("helloworld")语句就是体现了spring 的依赖注入特性,其中的helloworld 是spring容器中一个bean的id(仔细看bean.xml)。在通过spring的依赖注入之后找到了spring容器中的对应的bean之后就对其对象赋值了(仔细看bean.xml 文件中的 <property>标签。

一般情况下IOC和DI 基本是同时作用的,所有有人会觉得spring的DI 和IOC是一个特性,但实际上它们是两个不同的特性,只是在执行上或是代码上面没有界限区分。

在本文的例子中,首先通过依赖注入找到spring中对应的bean之后在对其实例化的对象进行赋值(赋值来源于<property的value中>)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值