关闭

简单实现spring里的BeanFactory实现原理

1155人阅读 评论(0) 收藏 举报
分类:

首先我们先创建一个BeanFactory借口:

/**
* Project Name:springAnalog
* File Name:BeanFactory.java
* Package Name:com.iflytek.beanfactory
* Date:2015-11-20下午09:40:50
* Copyright (c) 2015, syzhao@iflytek.com All Rights Reserved.
*
*/

package com.iflytek.beanfactory;

public interface BeanFactory
{
    /**
     * getBean:(定义一个获取bean的方法). <br/>
     * @author syzhao
     * @param id 
     * @return
     * @since JDK 1.6
     */
    public Object getBean(String id);
}


我们继续在创建一个实现BeanFactory类 ClassPathXmlApplicationContext这是最重要的:

/**
* Project Name:springAnalog
* File Name:ClassPathXmlApplicationContext.java
* Package Name:com.iflytek.beanfactory
* Date:2015-11-20下午09:43:20
* Copyright (c) 2015, syzhao@iflytek.com All Rights Reserved.
*
*/

package com.iflytek.beanfactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ClassPathXmlApplicationContext implements BeanFactory
{
    //定义一个filePath变量
    private String filePath;
    
    //首先定义Map 目的来存放生产的对象
    Map<String, Object> map = new HashMap<String, Object>();
    
    public ClassPathXmlApplicationContext(String filePath)
    {
        //获取配置文件路径
        this.filePath = filePath;
        //初始化
        try
        {
            this.readFile();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * readFile:(读取文件). <br/>
     * @author syzhao
     * @throws DocumentException 
     * @throws ClassNotFoundException 
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     * @since JDK 1.6
     */
    private void readFile()
        throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException
    {
        //接下来,用dom4j来解析,,,dom4j下载地址:http://sourceforge.net/projects/dom4j
        SAXReader reader = new SAXReader();
        //获取根节点
        Document document = reader.read(this.getClass().getClassLoader().getResourceAsStream(filePath));
        //获取bean所有的元素
        List<Element> list = document.selectNodes("/beans/bean");
        for (int i = 0; i < list.size(); i++)
        {
            //获取bean的id
            String id = list.get(i).attributeValue("id");
            //获取bean的class也就是类的路径
            String classpath = list.get(i).attributeValue("class");
            //利用反射获取对象  有三种方式,,,我们通过类的全名+包名来生成
            Object object = Class.forName(classpath).newInstance();
            
            //放到我们定义的map里 key =id ,vlaue =获取的对象,为下面getBean()取对象提供条件
            map.put(id, object);
        }
    }
    
    @Override
    public Object getBean(String id)
    {
        //这里根据id来获取对于的对象既可
        return map.get(id);
    }
    
}


接下来我们定义一个person借口

/**
* Project Name:springAnalog
* File Name:Person.java
* Package Name:com.iflytek.person
* Date:2015-11-20下午10:39:27
* Copyright (c) 2015, syzhao@iflytek.com All Rights Reserved.
*
*/

package com.iflytek.person;

public interface Person
{
    public void work();
}

我们用student和teacher实现person借口:

Student类:

/**
* Project Name:springAnalog
* File Name:Student.java
* Package Name:com.iflytek.person
* Date:2015-11-20下午10:40:42
* Copyright (c) 2015, syzhao@iflytek.com All Rights Reserved.
*
*/

package com.iflytek.person;

public class Student implements Person
{
    @Override
    public void work()
    {
        //输出一句话
        System.out.println("学生的工作是:认真学习!");
    }
    
}

Teacher类:

/**
* Project Name:springAnalog
* File Name:Teacher.java
* Package Name:com.iflytek.person
* Date:2015-11-20下午10:41:42
* Copyright (c) 2015, syzhao@iflytek.com All Rights Reserved.
*
*/

package com.iflytek.person;

public class Teacher implements Person
{
    @Override
    public void work()
    {
        //输出一句话
        System.out.println("老师的工作是:认真教书!!");
        
    }
    
}


接下来我们创建一个xml:applicationContext.xm

 

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="student" class="com.iflytek.person.Student"/>
    <bean id="teacher" class="com.iflytek.person.Teacher"/>
</beans>

好了,我们进行测试:

创建test类:

/**
* Project Name:springAnalog
* File Name:Test.java
* Package Name:com.iflytek
* Date:2015-11-20下午10:45:20
* Copyright (c) 2015, syzhao@iflytek.com All Rights Reserved.
*
*/

package com.iflytek;

import com.iflytek.beanfactory.BeanFactory;
import com.iflytek.beanfactory.ClassPathXmlApplicationContext;
import com.iflytek.person.Person;

public class Test
{
    /**
     * main:(这里用一句话描述这个方法的作用). <br/>
     * @author syzhao
     * @param args
     * @since JDK 1.6
     */
    public static void main(String[] args)
    {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person)beanFactory.getBean("student");
        person.work();
        person = (Person)beanFactory.getBean("teacher");
        person.work();
    }
    
}

当你运行时报了 java.lang.ClassNotFoundException: org.jaxen.JaxenException这个异常,不用怕,,

因为你少了,dom4j的另一个jar包:jaxen-1.1-beta-6.jar   这个在dom4j-1.6.1\lib下把jaxen-1.1-beta-6.jar引入既可以了,,

好了在运行一次:

结果完美呈现:


以上就是模拟了一下,,,其实spring内部远比这个复杂,需要大家努力去研究,,,,
























2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:53770次
    • 积分:1238
    • 等级:
    • 排名:千里之外
    • 原创:69篇
    • 转载:22篇
    • 译文:1篇
    • 评论:10条
    文章分类