IOC(Inversion of Control),即控制反转,它使你不需要再自己来实现对象的创建,而是把这些工作都交由容器来进行管理,增加了代码的可重用性。下面,便手动实现一个简单的IOC容器。
首先建立一个接口和这个接口的2个实现类:
- package cn.cgw.ioc;
- public interface ReportGenerator {
- public void generate(String[][] table);
- }
- <span style="white-space: normal;"><span style="white-space: pre;">package cn.cgw.ioc;</span></span>
- public class HtmlReportGenerator implements ReportGenerator {
- public void generate(String[][] table) {
- System.out.println("Generate HTML report...............");
- }
- }
- package cn.cgw.ioc;
- public class PdfReportGenerator implements ReportGenerator {
- public void generate(String[][] table) {
- System.out.println("Generate PDF report..................");
- }
- }
然后,我们建立一个名为ReportService的类,在这个类中需要到ReportGenerator接口的实现类对象,我们不再手工创建它,而是由IOC容器来管理,在这个类中采用setter方法进行注入。
- package cn.cgw.ioc;
- public class ReportService {
- private ReportGenerator reportGenerator;
- /**
- * 采用setter注入
- * @param reportGenerator
- */
- public void setReportGenerator(ReportGenerator reportGenerator) {
- this.reportGenerator = reportGenerator;
- }
- public void generateAnnualReport(int year) {
- String[][] statistics = null;
- //
- // Gather statistics for the year ...
- //
- reportGenerator.generate(statistics);
- }
- public void generateMonthlyReport(int year, int month) {
- String[][] statistics = null;
- //
- // Gather statistics for the month ...
- //
- reportGenerator.generate(statistics);
- }
- public void generateDailyReport(int year, int month, int day) {
- String[][] statistics = null;
- //
- // Gather statistics for the day ...
- //
- reportGenerator.generate(statistics);
- }
- }
下面我们建立一个属性文件,在属性文件中,定义了属性与对应类的映射关系:
- # Define a new component "reportGenerator"
- reportGenerator=cn.cgw.ioc.HtmlReportGenerator
- # Define a new component "reportService"
- reportService=cn.cgw.ioc.ReportService
- # Inject the component "reportGenerator" into property "reportGenerator"
- reportService.reportGenerator=reportGenerator
然后,我们实现IOC容器,在这个类中需要用到Apache的两个jar包。分别是common-logging和common-beanutils
- package cn.cgw.ioc;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Properties;
- import org.apache.commons.beanutils.PropertyUtils;
- public class Container {
- private Map<String,Object> components;
- public Container() {
- components = new HashMap<String,Object>();
- try {
- Properties properties = new Properties();
- //load properties file
- InputStream istr = this.getClass().getResourceAsStream("components.properties");
- properties.load(istr);
- for(Map.Entry entry : properties.entrySet()) {
- String key = (String)entry.getKey();
- String value = (String)entry.getValue();
- processEntry(key,value);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void processEntry(String key, String value) throws Exception{
- String[] parts = key.split("\\.");
- //new component definition
- if(parts.length == 1) {
- //reflection
- Object component = Class.forName(value).newInstance();
- components.put(parts[0], component);
- } else {
- // Dependency injection
- Object component = components.get(parts[0]);
- Object reference = components.get(value);
- PropertyUtils.setProperty(component,parts[1],reference);
- }
- }
- public Object getComponent(String id) {
- return components.get(id);
- }
- }
这样我们的工作就完成了,最后再写个测试方法来检验代码的正确性:
- Container container = new Container();
- ReportService reportService = (ReportService)container.getComponent("reportService");
- reportService.generateAnnualReport(2009);
可以看到,输出的结果为:
Generate HTML report...............
如果,我们将属性文件中reportGenerator=的值改为PdfReportGenerator,则输出的结果为:
Generate PDF report...............
通过以上结果我们可以清楚地认识到IOC的特性,我们并没有在ReportService类中创建ReportGenerator的任何实现类对象,这一切都是交由IOC容器进行管理的,通过属性文件的配置,我们可以轻松地更改想要使用的ReportGenerator实现类对象,它大大提高了代码的复用性。