依赖注入是指所依赖的对象不是由自己new出来的,而是用别的方式注入的.Spring的依赖注入是在BeanFactory创建bean的时候完成的.通常是在"bean"类中定义了"依赖"(成员变量,type为别的类,最好是interface或者abstract class),BeanFactory通过配置注入这些"依赖"(BeanFactory创建bean的时候赋值给这些成员变量).当这些"依赖"的实现发生变化的时候,不需要修改"bean"类,而只需要修改Spring的配置文件.或者通过配置新的"依赖"实现,实现新的业务而不用修改"bean"类.
比如有一个业务:对输入数据处理,返回结果,现有的类如下:
ProcessBea:Spring的bean类.
public class ProcessBean {
private Processor processor;
public Processor getProcessor() {
return processor;
}
public void setProcessor(Processor processor) {
this.processor = processor;
}
public Result process(InputData data) {
return processor.process(data);
}
}
bean类的依赖:
public interface Processor {
public Result process(InputData data);
}
public class InputData {
private String businessType;
private Map inputData = new HashMap();
public String getBusinessType() {
return businessType;
}
public void setBusinessType(String businessType) {
this.businessType = businessType;
}
public Map getInputData() {
return inputData;
}
public void setInputData(Map inputData) {
this.inputData = inputData;
}
}
public class Result {
String result;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
bean类的依赖的实现:
public class ProcessorImplA implements Processor {
public Result process(InputData data) {
Result rt = new Result();
// process steps by using input data...
rt.setResult("process result from ProcessorImplA");
return rt;
}
}
然后在配置文件中对bean进行配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="ProcessorImplA" class="com.test.spring.di.ProcessorImplA"/>
<bean name="ProcessorImplB" class="com.test.spring.di.ProcessorImplB"/>
<bean name="businessABean" class="com.test.spring.di.ProcessBean">
<property name="processor" ref="ProcessorImplA"/>
</bean>
</beans>
提供给调用者调用的类:
public class ProcessFacade {
private static BeanFactory factory;
static{
Resource resource = new ClassPathResource("conf/diAppcontext.xml");
factory = new XmlBeanFactory(resource);
}
public static Result testDI(InputData data){
ProcessBean processBean = (ProcessBean) factory.getBean(data.getBusinessType());
return processBean.process(data);
}
}
调用者通过如下代码调用:
InputData data = new InputData();
data.setBusinessType("businessABean ");
log.info(ProcessFacade.testDI(data).getResult());
当有新的处理业务的是候,通过提供一个新的依赖实现,把不同的依赖注入到bean类里面而支持新的业务.
一个新的依赖实现:
public class ProcessorImplB implements Processor {
public Result process(InputData data) {
Result rt = new Result();
// process steps by using input data...
rt.setResult("process result from ProcessorImplB");
return rt;
}
}
把不同的依赖注入到bean类里面:
<bean name="businessBBean" class="com.test.spring.di.ProcessBean">
<property name="processor" ref="ProcessorImplB"/>
</bean>
调用者通过如下代码调用新的业务:
InputData data = new InputData();
data.setBusinessType("businessBBean ");
log.info(ProcessFacade.testDI(data).getResult());
对如服务提供者(bean类,facade类)来说,不需要修改代码,而是通过注入不同的依赖,提供新的服务.
Spring通过配置bean的property(setter based dependency injection),bean的构造函数的参数或者工厂类的工厂方法的参数来实现注入(constructor based dependency injection).