熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

簡單模擬spring的容器管理bean

2013-11-23 20:37:17  來源: Java開源技術 
package comspring;

import javaioIOException;
import URL;
import javautilArrayList;
import javautilHashMap;
import javautilList;
import javautilMap;

import orgjdomDocument;
import orgjdomElement;
import orgjdomJDOMException;
import orgjdominputSAXBuilder;
import orgjdomxpathXPath;

import comvoBeanDefinition;

/**
 * 簡單模擬spring的ClassPathXmlApplicationContext spring容器管理bean
 * 
 * @author zdw
 * 
 */
@SuppressWarnings(unchecked)
public class MyApplicationContext
{
    private List<BeanDefinition> list = new ArrayList<BeanDefinition>();
    private Map<String Object> sigletons = new HashMap<String Object>();

    public MyApplicationContext(String fileName)
    {
        thisreadXML(fileName);
        try
        {
            instanceBeans();
        }
        catch (Exception e)
        {
            eprintStackTrace();
        }
    }

    /**
     * 實例化所有bean
     * 
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws ClassNotFoundException
     */
    private void instanceBeans() throws InstantiationException
            IllegalAccessException ClassNotFoundException
    {
        for (BeanDefinition bean : list)
        {
            if (null != beangetClassName() && beangetClassName()length() > )
                sigletonsput(beangetId() ClassforName(beangetClassName())
                        newInstance());
        }
    }

    /**
     * 讀取xml配置
     * 
     * @param fileName
     */
    public void readXML(String fileName)
    {
        SAXBuilder builder = new SAXBuilder();
        URL url = thisgetClass()getClassLoader()getResource(fileName);
        try
        {
            Document doc = builderbuild(url);
            XPath xpath = XPathnewInstance(//ns:beans/ns:bean);
            xpathaddNamespace(ns
                    );
            List<Element> beans = xpathselectNodes(doc);
            for (Element bean : beans)
            {
                String id = beangetAttributeValue(id);
                String className = beangetAttributeValue(class);
                BeanDefinition beanDefinition = new BeanDefinition(id
                        className);
                listadd(beanDefinition);
            }

        }
        catch (JDOMException e)
        {
            eprintStackTrace();
        }
        catch (IOException e)
        {
            eprintStackTrace();
        }
    }

    /**
     * 得到指定id的bean
     * 
     * @param id
     * @return
     */
    public Object getBean(String id)
    {
        return sigletonsget(id);
    }
}

  測試

@Test
    public void testMyUserService()
    {
        MyApplicationContext ctx = new MyApplicationContext(beansxml);
        UserService us = (UserService) ctxgetBean(userService);
        usprint();
    }

  代碼下載點擊


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28759.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.