Spring依赖注入的模拟实现

一 依赖注入和控制反转(DI & IoC)
1 依赖注入的模拟
//客户端代码
public class Business implements IBeanAware {
	private IWriter writer;

	public Business() {

	}

	// type1 Constructor Injection
	public Business(IWriter writer) {
		this.writer = writer;
	}

	// type2 Interface Injection
	@Override
	public void injectBean(Object dependcy) {
		writer = (IWriter) dependcy;
	}

	// type3 Setter Injection
	public void setWriter(IWriter writer) {
		this.writer = writer;
	}

	public void save() {
		writer.write();
	}

	public static void main(String[] args) throws Exception {
		BeanFactory applicationContext = new FileSystemBeanFactory(
				"simulation.properties");
		Business biz = (Business) applicationContext.getBean("business");
		biz.save();
	}
}

public interface IBeanAware {
	public void injectBean(Object dependcy);
}

// 组件接口
public interface IWriter {
	public void write();
}

//组件代码
public class FileWriter implements IWriter {

	@Override
	public void write() {
		System.out.println("write into file.");
	}
}

//组件代码
public class ConsoleWriter implements IWriter {

	@Override
	public void write() {
		System.out.println("write into console.");
	}
}

//模拟IoC容器接口
public interface BeanFactory {
	public Object getBean(String beanName) throws Exception;
}

//模拟IoC容器,管理依赖注入
public class FileSystemBeanFactory implements BeanFactory {
	private Object bean;
	private Properties props;

	public FileSystemBeanFactory(String contextFile) throws Exception {
		props = new Properties();
		props.load(new FileInputStream(contextFile));
	}

	@Override
	public Object getBean(String beanName) throws Exception {
		bean = BeanCreator.create(beanName, props);
		return bean;
	}
}

//模拟IoC容器,管理依赖注入
public class ClassPathBeanFactory implements BeanFactory {
	private Object bean;
	private Properties props;

	public ClassPathBeanFactory(String contextFile) throws Exception {
		props = new Properties();
		props.load(this.getClass().getResourceAsStream((contextFile)));
	}

	@Override
	public Object getBean(String beanName) throws Exception {
		bean = BeanCreator.create(beanName, props);
		return bean;
	}
}

public class BeanCreator {
	
	public static Object create(String beanName, Properties props)
			throws Exception {
		IWriter writer = (IWriter) Class.forName(props.getProperty("writer"))
				.newInstance();

		Class<?> claz = Class.forName(props.getProperty(beanName));
		Constructor<?> c = claz.getConstructor(IWriter.class);

		Object bean;

		if (c.getParameterTypes().length != 0) {
			bean = c.newInstance(writer);
			System.out.println("type1 Constructor Injection");
		} else if (IBeanAware.class.isAssignableFrom(claz)) {
			bean = claz.newInstance();
			IBeanAware aware = (IBeanAware) bean;
			aware.injectBean(writer);
			System.out.println("type2 Interface Injection");
		} else {
			bean = claz.newInstance();
			Method m = claz.getDeclaredMethod("setWriter", IWriter.class);
			m.invoke(bean, writer);
			System.out.println("type3 Setter Injection");
		}
		return bean;
	}
}


bean.properties
business = org.powersoft.spring.demo.Business
writer = org.powersoft.spring.demo.FileWriter
#writer = org.powersoft.spring.demo.ConsoleWriter

你可能感兴趣的:(java,spring,c,bean,IOC)