IOC浅析

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


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 又主要讲的是

构造函数注入 与 Set 注入

推荐Set注入

你可能感兴趣的:(spring,properties,String,IOC,Class,interface)