对spring框架的学习我是从模拟它的简单实现开始,这样也易于领悟到它的整个框架结构,以下是简单实现的代码:

配置文件:spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="beanId" class="pojo.Unit">
        <property name="name" value="cxhappyday"/>
        <property name="value" value="hello world!"/>
    </bean>
</beans>

 

程序入口:myspring.java

public class myspring {
    public static void main(String[] args) throws Exception{ 
        Unit unit = (Unit)(BeanFactory.getBean("D:\\……\mytest\\src\\test\\sprint.xml", "beanId"));
        System.out.print("unit is "+unit.toString()+",name:"+unit.getName()+",value:"+unit.getValue());
    }
}

 

实例:Unit.java

public class Unit {
    private String name; // 注意这里的name与以上xml文件的name是对用关系(必须相同)
    private String value; // 同上
    public Unit(){
        System.out.print("create Unit object\n");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

 

对象实例化的实现类:BeanFactory.java

package test;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class BeanFactory {

    // 获取实例化后的对象
    static public Object getBean(String path,String nodeName) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException, InvocationTargetException{
        Document document = loadXml(path);
        Element nodeElement = getNodeElement(document,nodeName);
        Object obj = reflectObject(nodeElement);
        setObjectAttr(obj,nodeElement);
        return obj;
    }

    // 加载xml文件
    static public Document loadXml(String path) throws DocumentException {
        SAXReader read = new SAXReader();
        return read.read(new File(path));
    }

    // 根据以上xml文件中配置的类全路径(pojo.Unit)使用反射得到实例
    static public Object reflectObject(Element node) throws InstantiationException, IllegalAccessException, ClassNotFoundException{
        return Class.forName(node.attributeValue("class")).newInstance();
    }

    
    static public Element getNodeElement(Document doc,String name){
        String nodeName = "//bean[@id='"+name+"']";
        return (Element)doc.selectSingleNode(nodeName);
    }

    // 根据spring.xml中的配置设置Unit对象的属性值即name和value的值
    static public void setObjectAttr(Object obj,Element element) throws IllegalAccessException, InvocationTargetException{
        List<Element> list = element.elements("property");
        for (Element node:list){
            String name = node.attributeValue("name");
            String value = node.attributeValue("value");

            // 这里关注下 BeanUtils工具类的使用
            BeanUtils.setProperty(obj, name, value);
        }
    }
}

最后说下运行该程序需要的几个Jar包:

1、commons-beanutils-1.8.0.jar:BeanUtils类在该Jar中负责给对象属性赋值;

2、commons-logging.jar:commons-beanutils-1.8.0.jar对它依赖;

3、dom4j-1.6.1.jar:负责解析XML文件;

4、jaxen-1.1-beta-6.jar:dom4j-1.6.1.jar依赖它;

Spring笔记之(一)初探的更多相关文章

  1. Spring笔记02_注解_IOC

    目录 Spring笔记02 1. Spring整合连接池 1.1 Spring整合C3P0 1.2 Spring整合DBCP 1.3 最终版 2. 基于注解的IOC配置 2.1 导包 2.2 配置文件 ...

  2. Spring笔记01_下载_概述_监听器

    目录 Spring笔记01 1.Spring介绍 1.1 Spring概述 1.2 Spring好处 1.3 Spring结构体系 1.4 在项目中的架构 1.5 程序的耦合和解耦 2. Spring ...

  3. Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)

    Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...

  4. Spring笔记:事务管理

    Spring笔记:事务管理 事务管理 Spring事务管理是通过SpringAOP去实现的.默认情况下Spring在执行方法抛出异常后,引发事务回顾,当然你可以用拦截器或者配置去改变它们. 这部门内容 ...

  5. Spring笔记:AOP基础

    Spring笔记:AOP基础 AOP 引入AOP 面向对象的开发过程中,我们对软件开发进行抽象.分割成各个模块或对象.例如,我们对API抽象成三个模块,Controller.Service.Comma ...

  6. Spring:笔记整理(1)——HelloWorld

    Spring:笔记整理(1)——HelloWorld 导入JAR包: 核心Jar包 Jar包解释 Spring-core 这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件 ...

  7. Spring笔记:IOC基础

    Spring笔记:IOC基础 引入IOC 在Java基础中,我们往往使用常见关键字来完成服务对象的创建.举个例子我们有很多U盘,有金士顿的(KingstonUSBDisk)的.闪迪的(SanUSBDi ...

  8. Spring笔记(6) - Spring的BeanFactoryPostProcessor探究

    一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...

  9. spring揭秘 读书笔记 一 IoC初探

    本文是王福强所著<<spring揭秘>>一书的读书笔记 ioc的基本概念 一个例子 我们看下面这个类,getAndPersistNews方法干了四件事 1 通过newsList ...

随机推荐

  1. ZeroMQ/jzmq安装使用

    环境: No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 12.04.2 LTS Release: 12 ...

  2. python log 层次结构

    文件结构 - run.py - b -- __init__.py run.py import logging import b log = logging.getLogger("" ...

  3. Python和C++交互

    关键字:Python 2.7,VS 2010,swig OS:Win8.1 with update. 1.下载swig:http://www.swig.org/download.html 2.将swi ...

  4. python编程语言 函数的形参

    python编程语言 函数的形参的讲解: 我在交互模式中写了个函数: def adder(**args): sum=0 for x in args.keys(): sum+=args[x] retur ...

  5. 为WPF版的GridControl控件添加行序号功能

    废话不多数,先上效果图和代码: 包装GridControl控件 cs using DevExpress.Xpf.Data; using DevExpress.Xpf.Grid; using Syste ...

  6. STL的pair学习, map学习

    http://blog.csdn.net/calvin_zcx/article/details/6072286 http://www.linuxidc.com/Linux/2014-10/107621 ...

  7. 4、Hbase

    1).逻辑模型 Hbase 以表的形式存储数据,每个表由行和列组成,每个列属于一个特定的列族. 表中由行和列确定的存储单元称为一个元素,每个元素保存了同一份数据的多个版本,由时间戳来标识.行健是数据行 ...

  8. MVC-处理时间格式

    第一种方法:先设置一个时间显示的模板,然后在需要显示时间的地方调用这个模板就行了. 1.在Share文件夹下,创建一个文件夹DisplayTemplates 2.在DisplayTemplates文件 ...

  9. const变量的存储区及修改权限

    转自const变量的存储区及修改权限 [cpp] view plaincopy const int a = 1; int *p = const_cast<int*>(&a); *p ...

  10. Win7 & Win 8系统更新失败的解决

    转自:Win 8系统更新失败的解决(原创) 这几天win 8又出了一大堆更新,而且是一更新完就要重启,重启之后照例要进入更新包的安装过程.不爽的是,屡屡在重启后出现"配置Windows更新失 ...