简单模拟spring的容器管理bean

package  com.spring;

import  java.io.IOException;
import  java.net.URL;
import  java.util.ArrayList;
import  java.util.HashMap;
import  java.util.List;
import  java.util.Map;

import  org.jdom.Document;
import  org.jdom.Element;
import  org.jdom.JDOMException;
import  org.jdom.input.SAXBuilder;
import  org.jdom.xpath.XPath;

import  com.vo.BeanDefinition;

/** */ /**
 * 简单模拟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)
    
{
        
this.readXML(fileName);
        
try
        
{
            instanceBeans();
        }

        
catch (Exception e)
        
{
            e.printStackTrace();
        }

    }


    
/** *//**
     * 实例化所有bean
     * 
     * 
@throws InstantiationException
     * 
@throws IllegalAccessException
     * 
@throws ClassNotFoundException
     
*/

    
private void instanceBeans() throws InstantiationException,
            IllegalAccessException, ClassNotFoundException
    
{
        
for (BeanDefinition bean : list)
        
{
            
if (null != bean.getClassName() && bean.getClassName().length() > 0)
                sigletons.put(bean.getId(), Class.forName(bean.getClassName())
                        .newInstance());
        }

    }


    
/** *//**
     * 读取xml配置
     * 
     * 
@param fileName
     
*/

    
public void readXML(String fileName)
    
{
        SAXBuilder builder 
= new SAXBuilder();
        URL url 
= this.getClass().getClassLoader().getResource(fileName);
        
try
        
{
            Document doc 
= builder.build(url);
            XPath xpath 
= XPath.newInstance("//ns:beans/ns:bean");
            xpath.addNamespace(
"ns",
                    
"http://www.springframework.org/schema/beans");
            List
<Element> beans = xpath.selectNodes(doc);
            
for (Element bean : beans)
            
{
                String id 
= bean.getAttributeValue("id");
                String className 
= bean.getAttributeValue("class");
                BeanDefinition beanDefinition 
= new BeanDefinition(id,
                        className);
                list.add(beanDefinition);
            }


        }

        
catch (JDOMException e)
        
{
            e.printStackTrace();
        }

        
catch (IOException e)
        
{
            e.printStackTrace();
        }

    }


    
/** *//**
     * 得到指定id的bean
     * 
     * 
@param id
     * 
@return
     
*/

    
public Object getBean(String id)
    
{
        
return sigletons.get(id);
    }

}

测试:
@Test
    
public   void  testMyUserService()
    
{
        MyApplicationContext ctx 
= new MyApplicationContext("beans.xml");
        UserService us 
= (UserService) ctx.getBean("userService");
        us.print();
    }
下载地址1
下载地址2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值