Spring依赖注入的模拟实现

本文通过实例演示了依赖注入(DI)与控制反转(IoC)的概念及其实现方式,包括构造器注入、接口注入与设置器注入三种类型。通过模拟IoC容器,展示了如何在不同场景下进行依赖管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 依赖注入和控制反转(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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值