首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring依赖注入的模拟兑现

2012-10-05 
Spring依赖注入的模拟实现一 依赖注入和控制反转(DI & IoC)1 依赖注入的模拟//客户端代码public class Bus

Spring依赖注入的模拟实现
一 依赖注入和控制反转(DI & IoC)
1 依赖注入的模拟

//客户端代码public class Business implements IBeanAware {private IWriter writer;public Business() {}// type1 Constructor Injectionpublic Business(IWriter writer) {this.writer = writer;}// type2 Interface Injection@Overridepublic void injectBean(Object dependcy) {writer = (IWriter) dependcy;}// type3 Setter Injectionpublic 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 {@Overridepublic void write() {System.out.println("write into file.");}}

//组件代码public class ConsoleWriter implements IWriter {@Overridepublic 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));}@Overridepublic 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)));}@Overridepublic 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.propertiesbusiness = org.powersoft.spring.demo.Businesswriter = org.powersoft.spring.demo.FileWriter#writer = org.powersoft.spring.demo.ConsoleWriter

热点排行