spring BeanFactory的xml工厂模式简单实现(二)

背景

工厂模式是你们很是熟悉的设计模式,spring BeanFactory将此模式发扬光大,今天就来说讲,spring IOC容器是如何帮我管理Bean,构造对象的,简单的写了一个demo。java

思路以下:spring

  1. 解析xml文件,将bean标签的,id,class存储在一个map中
  2. 获取实例对象时经过id匹配,匹配成功返回该对象

先看下常见的工厂模式

关于什么是工厂模式以前写过一个:http://www.javashuo.com/article/p-fbilqccu-ek.html设计模式

建立一个接口类Pizza

package com.spring.xml;

public interface Pizza{
    public float getPrice();
}

MargheritaPizza 类

package com.spring.xml;

public class MargheritaPizza implements Pizza{
    public float getPrice() {
        System.out.println("8.5f");
        return 8.5f;
       
    }
}

CalzonePizza 类

public class CalzonePizza implements Pizza{
   public float getPrice() {
       System.out.println("2.5f");
       return 2.5f;
   }
   

}

创建工厂类PizzaFactory

经过传入参数id,选择不一样的实例类,若是后续不断的增长新类,会频繁的修改create方法,不符合开闭原则app

public class PizzaFactory {
    public Pizza create(String id) {
        if (id == null) {
          throw new IllegalArgumentException("id is null!");
        }
        if ("Calzone".equals(id)) {
          return new CalzonePizza();
        }

        if ("Margherita".equals(id)) {
          return new MargheritaPizza();
        }

        throw new IllegalArgumentException("Unknown id = " + id);
      }

}

采用spring xml 配置的方式实现工厂类的管理

创建一个BeanFactory接口

package com.spring.xml;
2 
3 public interface BeanFactory {
4     Object getBean(String id);
5 }

实现类ClassPathXmlApplicationContext.java

BeanFactory实现类,主要功能是解析xml文件,封装bean到map中dom

public class ClassPathXmlApplicationContext implements BeanFactory {
     private Map<String, Object> beans = new HashMap<String, Object>();
     public ClassPathXmlApplicationContext(String fileName) throws Exception{
         SAXReader reader = new SAXReader();
         Document document =     reader.read(this.getClass().getClassLoader().getResourceAsStream(fileName));
         List<Element> elements = document.selectNodes("/beans/bean");
        for (Element e : elements) {
           //获取beanId
            String id = e.attributeValue("id");
           //获取beanId对于的ClassPath下class全路径名称
             String value = e.attributeValue("class");
            //经过反射实例化该beanId 的class对象
            Object o = Class.forName(value).newInstance();
            //封装到一个Map里
             beans.put(id, o);
         }
     }
     
     public Object getBean(String id) {
         return beans.get(id);
    }
 
 }

applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>
 <beans>
     <bean id="calzone" class="com.spring.xml.CalzonePizza"></bean>
      <bean id="margherita" class="com.spring.xml.MargheritaPizza"></bean>
 </beans>

写一个类测试一下测试

package com.spring.xml;
 
 import org.dom4j.DocumentException;
 
 public class Test {

    public static void main(String[] args) throws Exception {
         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
         Pizza p = (Pizza)factory.getBean("Calzone");
         p.getPrice();
        
     }
 
}

看完代码应该很是清楚,比起原先经过if else来选择不一样的实现类方便。this

相关文章
相关标签/搜索