你的位置:首页 > ASP.net教程

[ASP.net教程]设计模式之工厂模式(3)


奔主题:模拟SpringBean工厂:

先来一个简单的interface:beanFactory

package cn.asto.spring;public interface BeanFactory {  public Object getBean();}

一个最简单的Spring配置文件:

<??><beans ="http://www.springframework.org/schema/beans"    ="http://www.w3.org/2001/    xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="v" class="cn.asto.spring.Car"> </bean></beans>

现在我们已经有了配置文件,有了BeanFactroy接口,接下来要做的就是实现一个BeanFactory的接口。
这个实现中做那么几件事情:

  • 读入配置文件
  • 利用反射将类名字符串实例化成对象
  • 将对象注入Map(这个Map就是Spring的Bean容器)
  • 通过<key,value>的形式获取对象

 


 

 

  • 读入配置文件

我这里使用JDOM(利用了XPath语法):

package cn.asto.spring;import java.util.HashMap;import java.util.List;import java.util.Map;import org.jdom2.Document;import org.jdom2.Element;import org.jdom2.filter.Filters;import org.jdom2.input.SAXBuilder;import org.jdom2.xpath.XPathExpression;import org.jdom2.xpath.XPathFactory;public class ClassPathimplements BeanFactory{     public ClassPaththrows Exception{    //读入        SAXBuilder sax = new SAXBuilder();     Document doc = sax.build(ClassPathclass.getClassLoader().getResource(= XPathFactory.instance();    XPathExpression<Element> expr = xFactory.compile("/beans/bean", Filters.element());    List<Element> beans = expr.evaluate(doc);    for (Element bean : beans) {      String id = bean.getAttributeValue("id");      String value = bean.getAttributeValue("class");      }  }  @Override  public Object getBean(String key) {        return null;  }}

 

  • 利用反射将类名 字符串实例化成对象
  • 将对象注入Map(这个Map就是Spring的Bean容器)
  • 通过<key,value>的形式获取对象
    package cn.asto.spring;import java.util.HashMap;import java.util.List;import java.util.Map;import org.jdom2.Document;import org.jdom2.Element;import org.jdom2.filter.Filters;import org.jdom2.input.SAXBuilder;import org.jdom2.xpath.XPathExpression;import org.jdom2.xpath.XPathFactory;public class ClassPathimplements BeanFactory{      private Map<String,Object> map = new HashMap<String,Object>();  public ClassPaththrows Exception{    //读入        SAXBuilder sax = new SAXBuilder();     Document doc = sax.build(ClassPathclass.getClassLoader().getResource(= XPathFactory.instance();    XPathExpression<Element> expr = xFactory.compile("/beans/bean", Filters.element());    List<Element> beans = expr.evaluate(doc);    for (Element bean : beans) {      String id = bean.getAttributeValue("id");      String value = bean.getAttributeValue("class");      System.out.println(value);      //反射实例化对象      Object o = Class.forName(value).newInstance();      //经典IOC注入容器      map.put(id, o);    }  }  @Override  public Object getBean(String key) {    //得到Bean    return map.get(key);  }}

     

测试一下:

package cn.asto.spring;public class Test {  public static void main(String args[]) throws Exception {    BeanFactory factory = new ClassPath);    Car car = (Car)factory.getBean("v");    car.move();  }}

输出:

car is running.

ok,spring的IOC模拟完成!

 

哦。忘记把Car类放上来了:

package cn.asto.spring;public class Car {  public void move(){    System.out.println("car is running");  }}