对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. MVC-内容详情页显示内容

    @model InfoDataProvider.DataModel.FAQ_ContentUser 内容Content字段:如果里面有html标签. @Html.DisplayFor(p => ...

  2. 导入NGUI插件

    在Unity编辑器顶部菜单栏中的Assets菜单中选择Import Package,然后选择Custom Package(自定义资源包),弹出资源路径窗口,在其中找到NGUI资源包所在的位置,单击”打 ...

  3. android 界面布局 很好的一篇总结[转]

    1.LinearLayout ( 线性布局 ) :(里面只可以有一个控件,并且不能设计这个控件的位置,控件会放到左上角) 线性布局分为水平线性和垂直线性二者的属性分别为:android:orienta ...

  4. 让Eclipse使用新版本的JRE

    更新到新的 Mac OS X 再打开Eclipse 编译程序会报错, Exception in thread "main" java.lang.UnsupportedClassVe ...

  5. 关于hibernate的实体类中有集合类型转化成JSON的工具类 - 怀念今天的专栏 - 博客频道

    Json 来源:http://blog.csdn.net/zczzsq/article/details/18697045#1536434-hi-1-4387-42d97150898b1af15ddaa ...

  6. ***PHP各种编码的汉字字符串截取

    虽然PHP有现成的截取字符串函数substr(),但是这个函数不能对汉字字符串进行截取,要实现这种效果还需要我们自己去编写相应的函数.汉字有多种编码,比如GB2312,UTF-8等,汉字字符串的截取需 ...

  7. 使用正则表达式匹配HTML 下各种<title>标签

    http://www.oschina.net/question/195686_46313 <title>标题</title> <title>标题</title ...

  8. Java装饰设计模式的例子

    这里给出一个顾客购买咖啡的例子.其中咖啡可以加冰(2元),加巧克力(4元). 下面是面向对象中装饰模式的解决方案. /** * Created with IntelliJ IDEA. * User: ...

  9. grab jpegs from v4l2 devices

    某些平台下opencv不能直接直接支持摄像头获取图片或视频,这是需要使用v4l2(video for linux 2) 测试v4l2是否可以读到摄像头的内容,最简单的办法就是读取一张截图. v4l2g ...

  10. ASP.NET 弹出对话框和页面之间传递值的经验总结

    今天碰到一个弹出对话框(PopUp dialog)的问题, 因该是个傻瓜问题, 但是还是让我研究了半天, 总结了一些前人经验, 拿出来跟大家分享一下! 在ASP.Net中页面之间的传值方法有很多,但是 ...