【Spring专题-①】IOC浅析

声明:内容并非原创,主要来自《Spring2.0技术手册》一书


Spring 的主要优点:
1 对Bean 的管理
2 不依赖于底层实现,而依赖于抽象,依赖于接口,依赖于注入 


前提:
1 当组件出现重用时,再考虑使用Spring IoC
2 当组件用到外部资源时,使用Spring IoC


3 不要为IoC而IoC
1 IoC
Inversion of Control,常译为控制反转。


简单的说,高层不依赖于底层实现,而是依赖于接口。
举个简单的例子,比如电视机,最基本的条件是有电才能开机。
用Spring的思想来解释就是不关心底层电是如何产生的,并不需要拿一个发电机来才能看电视,给我一个(电的)接口也就是插座就行。
借用一句英文来概括:Don't call me,I'll call you


IOC: 依赖注入

这里主要通过举简单的例子,来试图解释IOC的复杂原理。


没有IOC的代码:



高层应用调用底层,应用依赖底层模块。


改进1

1> 引入接口

public interface IDeviceWrite {
	public void saveToDevice();
}

2>接口实现类

public class FloppyImpl implements IDeviceWrite {

	public void saveToDevice() {
		// TODO Auto-generated method stub
		System.out.println("Save to Floppy...");
	}

}

3>上层应用

public class Business {
	
	public void save(IDeviceWrite write){
		write.saveToDevice();
	}
	
	public static void main(String[] args){
		Business business = new Business();
		IDeviceWrite floppy = new FloppyImpl();
		business.save(floppy);
		
	}
}

可以看到通过引入接口上层与底层的依赖解耦了,不依赖于底层实现,面向接口更灵活了


改进2:

在改进1的基础上增加了配置文件,通过修改配置文件就可以改变上层应用引用底层的实例,更加灵活。

public class BusinessFactory {
	private IDeviceWrite write;
	private static BusinessFactory instance = null;
	
	private BusinessFactory(){
		this.setWrite();
	}
	public IDeviceWrite getWrite() {
		return write;
	}
	public void setWrite() {
		Properties prop = new Properties();
		try{
			prop.load(new FileInputStream("./conf.properties"));
			String deviceClass = prop.getProperty("writer.class");
			write = (IDeviceWrite)Class.forName(deviceClass).newInstance();
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}catch (IllegalAccessException e) {
			e.printStackTrace();
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		}catch (InstantiationException e) {
			e.printStackTrace();
		}
	}
	
	public static synchronized BusinessFactory getInstance(){
		if(null == instance)
			return new BusinessFactory();
		return instance;
	}
	
	public static void main(String[] args){
		BusinessFactory business = getInstance();
		IDeviceWrite write = business.getWrite();
		write.saveToDevice();
	}
	
}

4>几个主要的类

org.springframework.context.ApplicationContext

org.springframework.context.support.ClassPathXmlApplicationContext

org.springframework.context.support.FileSystemXmlApplicationContext


Instantiating a container

// create and configure beans
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
// retrieve configured instance
PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);
// use configured instance
List userList service.getUsernameList();



5> The following example shows the basic structure of XML-based configuration metadata

<?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-3.0.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->

</beans>


【编者按】

1 IOC (inversion of Control 控制反转)

在reference中IOC这部分主要讲的是DI(dependece Inject)

DI (Dependency Injection 依赖注入)又主要讲的是

构造函数注入 与 Set 注入

推荐Set注入

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值