模拟spring容器

项目包括4个类(BeanDefinition,Person, MyClassPathXMLApplicationContext,test),1个xml文件(beans.xml)以及一些需要引入的包;
commons-logging-1.2.jar
dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar
spring-aop-4.2.5.RELEASE.jar
spring-beans-4.2.5.RELEASE.jar
spring-context-4.2.5.RELEASE.jar
spring-core-4.2.5.RELEASE.jar
spring-expression-4.2.5.RELEASE.jar
spring-test-4.2.5.RELEASE.jar
spring-tx-4.2.5.RELEASE.jar

-----------------------------------------------华丽的分界线---------------------------------------------------------------

package hw;
//定义bean的类
public class  BeanDefinition {  

    private String id;    
    private String className;  

    public BeanDefinition(String id, String className) {  
        this.id = id;  
        this.className = className;  
    }  

    public String getId() {  
        return id;  
    }  

    public void setId(String id) {  
        this.id = id;  
    }  

    public String getClassName() {  
        return className;  
    }  

    public void setClassName(String className) {  
        this.className = className;  
    }  
}  

-----------------------------------------------华丽的分界线---------------------------------------------------------------

package hw;
//实体类
public class  Person {
    private String name;
    private String gender;
    private String age;

    public String getName() {
        return name;
    }
    public String setName(String name) {
        this.name = name;
        return name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }

    public void show() {
        System.out.println("Person name:" + name + " gender:" + gender + " age:" + age);
    } 
}

-----------------------------------------------华丽的分界线---------------------------------------------------------------

package hw;

import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.jaxen.NamespaceContext;


public class  MyClassPathXMLApplicationContext {
    private List<BeanDefinition> beanDefines =new ArrayList<BeanDefinition>();  
    private Map<String,Object> sigletons =new HashMap<String,Object>();  

    public MyClassPathXMLApplicationContext(String xmlDocFileName){  
            this.processXmlDoc(xmlDocFileName);  
        }  

    private void processXmlDoc(String xmlDocFileName) {  
        SAXReader saxReader = new SAXReader();
        Document document =null;  
        try{  
            URL xmlpath = this.getClass().getClassLoader().getResource(xmlDocFileName);  
            document =saxReader.read(xmlpath);  
            Map<String,String> nsMap =new HashMap<String,String>();  
            nsMap.put("ns"," http://www.springframework.org/schema/beans"); //加入命名空间  
            XPath xsub = document.createXPath("//ns:beans/ns:bean"); //创建beans/bean查询路径  
            xsub.setNamespaceURIs(nsMap); // 设置命名空间  
            List<Element> beans = xsub.selectNodes(document); // 获取文档下所有的bean节点  
            for(Element element:beans){  
                String id =element.attributeValue("id"); // 获取id属性值  
                String clazz =element.attributeValue("class"); // 获取class属性值  
                BeanDefinition beanDefine =new BeanDefinition(id,clazz);  
                beanDefines.add(beanDefine);
                Object o = getProperty(element,clazz);
                sigletons.put(id,o);
            }    
        } catch(Exception e) {  
            e.printStackTrace();  
        }  
    }  

    private Object getProperty(Element e,String classname) throws Exception {
        String methodname = null;
        String arg = null;
        Object obj = Class.forName(classname).newInstance();
        Iterator<Element> bean = e.elementIterator(); 
        while(bean.hasNext()) {
            Element pro = (Element)bean.next();
            if(pro.getName() == "property") {
                if(pro.getTextTrim() != null) {
                    if(pro.attributeValue("name") != null) {
                        methodname = pro.attributeValue("name");
                    }
                    if(pro.attributeValue("value") != null) {
                        arg = pro.attributeValue("value");
                    }else {
                        arg = getValue(pro);
                    }
                    obj = invokeMethod(obj,methodname,arg);
                }
            }
        }
        return obj;
    }

    private Object invokeMethod(Object obj,String methodname,String arg)throws Exception{
        String m = "set" + methodname.substring(0, 1).toUpperCase()+methodname.substring(1).toLowerCase();
        Method method = obj.getClass().getMethod(m, String.class);
        method.invoke(obj,arg);
        return obj;
    }

    private String getValue(Element pro){
        String arg = null;
        Iterator<Element> property = pro.elementIterator();
        if(property.hasNext()) {
            Element val = (Element)property.next();
            if(val.getName() == "value") {
                if(val.getTextTrim() != null) {
                    arg = val.getTextTrim();
                }
            }
        }
        return arg;
    }

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

-----------------------------------------------华丽的分界线---------------------------------------------------------------
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
       xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans

    <bean id="person1" class="hw.Person">
        <property name="name">
            <value>wen</value>
        </property>
        <property name="gender" >
            <value>female</value>
        </property>
        <property name="age">
            <value>20</value>
        </property> 
    </bean>

    <bean id="person2" class="hw.Person">
        <property name="name" value="ken" />
        <property name="gender" value = "male" />
        <property name="age" value="33" />
    </bean>

    <bean id="person3" class="hw.Person">
        <property name="name">
            <value>lin</value>
        </property>
        <property name="gender" >
            <value>female</value>
        </property>
        <property name="age">
            <value>12</value>
        </property> 
    </bean>

</beans>

-----------------------------------------------华丽的分界线---------------------------------------------------------------

package hw;

import static org.junit.Assert.*;

import org.junit.Test;

public class  test {

    @Test
    public void test() {
        MyClassPathXMLApplicationContext ctx = new MyClassPathXMLApplicationContext("beans.xml");
        Person p1 = (Person)ctx.getBean("person1");
        System.out.println(p1.getName());
        System.out.println(p1.getGender());
        System.out.println(p1.getAge());
        p1.show();
        System.out.println("--------------------------------");
        Person p2 = (Person)ctx.getBean("person2");
        System.out.println(p2.getName());
        System.out.println(p2.getGender());
        System.out.println(p2.getAge());
        p2.show();
        System.out.println("--------------------------------");
        Person p3 = (Person)ctx.getBean("person3");
        System.out.println(p3.getName());
        System.out.println(p3.getGender());
        System.out.println(p3.getAge());
        p3.show();
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值