自己重写Spring_XmlBeanFactory

 
自己重写Spring_XmlBeanFactory
2007年11月25日 星期日 01:53
自己重写Spring_XmlBeanFactory
-----------------------------------------------------------------------------------------
HelloWorld.java
-----------------------------------------------------------------------------------------
public interface HelloWorld {

    void setName(String name);

    String say();

}
-----------------------------------------------------------------------------------------
HelloWorldImpl.java
-----------------------------------------------------------------------------------------

public class HelloWorldImpl implements HelloWorld {

    private String >
    public void setName(String name) {
        this.name = name;

    }

    public String say() {
        if (name == null)
            return "Hello,world!";
        return "Hello," + name + "!";
    }

}

-----------------------------------------------------------------------------------------
SpringStartup.java
-----------------------------------------------------------------------------------------
public class SpringStartup {

    /**
    * @param args
    */
    public static void main(String[] args) {
        XmlBeanFactory factory = new XmlBeanFactory("beans.xml");
        HelloWorld hello = (HelloWorld) factory.getBean("a");

        System.out.println(hello.say());
     

    }

}
-----------------------------------------------------------------------------------------
beans.xml
-----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans>

<bean >
        <property value="a" />

    </bean>

<bean >
        <property value="xiong" />

    </bean>


</beans>
-----------------------------------------------------------------------------------------
XmlBeanFactory.java
-----------------------------------------------------------------------------------------
import java.lang.reflect.Method;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlBeanFactory {

    private Properties pro;

    private Document document = null;

    private static String XMLFILE = null;

    private static String key = "key";

    public XmlBeanFactory(String XMLFILE) {
        this.XMLFILE = XMLFILE;
        init();

    }

    private void init() {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(XMLFILE);
        } catch (Exception ex) {

            ex.printStackTrace();
        }
    }

    // 解析XML文件
    private Properties xmlParser(String nodeName, String id_key, String id_value) {

        Properties pro = new Properties();
        try {

            NodeList list = document.getElementsByTagName(nodeName);
            for (int k = 0; k < list.getLength(); k++) {

                Node fatherNode = list.item(k);

                NamedNodeMap attributes = fatherNode.getAttributes();
                //
                boolean flag = false;
                for (int i = 0; i < attributes.getLength(); i++) {

                    Node attribute = attributes.item(i);

                    if (attribute.getNodeName().equals(id_key)
                            && attribute.getNodeValue().equals

(id_value)) {
                        flag = true;

                    }

                }
                if (!flag)
                    continue;
                //
                for (int i = 0; i < attributes.getLength(); i++) {

                    Node attribute = attributes.item(i);

                    pro.setProperty(attribute.getNodeName(), attribute
                            .getNodeValue());
                }
                //
                NodeList childNodes = fatherNode.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {

                    Node childNode = childNodes.item(j);

                    if (childNode instanceof Element) {

                        NamedNodeMap child_attributes = childNode
                                .getAttributes();

                        for (int i = 0; i <

child_attributes.getLength(); i++) {

                            Node child_attribute =

child_attributes.item(i);

                            pro.setProperty

(child_attribute.getNodeName(),
                                   

child_attribute.getNodeValue());
                        }
                    }
                }
                break;
            }

            return pro;
        } catch (Exception ex) {

            ex.printStackTrace();
            return null;
        }
    }

    public Object getBean(String id_value) {
        String nodeName = "bean";
        Properties pro = xmlParser(nodeName, "id", id_value);

        return ClassLoadFactory.loadClassByConstructor(pro);

    }

}

/** 类装载工厂 */
class ClassLoadFactory {

    public static Object loadClassByConstructor(Properties pro) {
        try {

            Class cl = Class.forName(pro.getProperty("class"));

            Object obj = cl.newInstance();
            Class[] p = { String.class };
            //
            String name");
            String methodName = name.substring(0, 1).toUpperCase()
                    + name.substring(1, name.length());
            Method method = cl.getMethod("set" + methodName, p);
            Object[] value = { pro.getProperty("value") };
            method.invoke(obj, value);
            //
            return obj;
        } catch (Exception ex) {

            ex.printStackTrace();
            return null;
        }

    }
}
-----------------------------------------------------------------------------------------

本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源   自己重写Spring_XmlBeanFactory_熊熊之家
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
计算机网络期末复习题 201708-201801计算机网络复习指导发布-校考学生!!下载截止于12.5日晚上9点 2017-12-04 23:07 计算机网络复习指导-校内考试 §1.名词解释: 1. 计算机网络 2.网络体系结构 3.高速以太网 4.静态路由选择 5. 动态路由选择 6.子网掩码 8.动态主机配置协议 9.套接字 10.客户/服务器模型(C/S模型) 11.域名系统(DNS) 12.光纤分布式数据接口FDDI §2.简答题: 1.试谈你对网络安全的威胁和防病毒技术的认识。(线下教学课件) 2.简述物理层的四个特性。(线下教学课件) 3.计算机网络的组成(P2-P3) 4.计算机网络的功能(P3) 5.计算机网络标准化的相关组织(P4+线下教学课件) 6.计算机网络的性能指标(P8+线下教学课件) 7.简述你所知道的网络传输介质(P33-34) 8.物理层的四个特性(P34) 9.简述四种帧边界的划分方法(即组帧方法)。(P52-54+线下教学课件) 10.简述停止-等待流量控制(协议)。(P59+P62-P64+线下教学课件) 11.简述时分多路复用(P69+线下教学课件) 12.简述频分多路复用(P68+线下教学课件) 13.简述IPv4地址的分类(P122-123) 14.UDP数据报与IP分组的区别(P187) §3.论述题: 1.试说明数据链路层原语和协议的转换。(线下教学课件) 2.描述常规释放的过程,并画出其示意图。(线下教学课件) 3.奈奎斯特定理公式和香农定理公式的主要区别是什么? 4.试比较电路交换、报文交换和分组交换等三种数据传输方式。(P27-29) 5.谈谈你对差错控制方法的理解。(P54-58+线下教学课件) 6.谈谈域名解析过程(P225-226) 7.ARP和DNS是否有些相似?它们有何区别?(P227) 8.FTP的工作原理。(P228)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值