对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. nuget的使用总结

    使用NuGet发布自己的类库包(Library Package) from:http://blog.csdn.net/gulijiang2008/article/details/41724927 使用 ...

  2. SQL常用命令浅析

    表操作备注:操作之前使用“use <数据库名>”应连接某个数据库.建表命令:create table <表名> (<字段名 1> <类型 1> [,.. ...

  3. HttpContext

    HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息.也有人叫上下文信息. 1.生存周期:从客户端用户点击并产生了一个向服务器发送请求开始---服务器处理完请求并生成返 ...

  4. 按行N等分某个文件

    # --*-- coding:utf-8 --*--import randomimport math def fanhui():    into = random.randint(1, 10)    ...

  5. Postman(API & HTTP请求调试插件)和Apizza fiddler

    http://blog.csdn.net/u011012932/article/details/51456263#comments

  6. input checkbox问题和li里面包含checkbox

    <input type="checkbox" id="checkbox1"/> $("input#checkbox1").cli ...

  7. Form.KeyPreview 属性2

    在使用.Net Framework编写窗体应用程序的时候,有时有需要响应窗体的按键消息. 当窗体上没有任何其他控件的时候,窗体是可以直接响应这些消息的. 但是当窗体上有其他控件时,会发现窗体再也不会响 ...

  8. Toad for Oracle 12 download link

    Toad for Oracle 12 download link x64-bit http://us-downloads.quest.com/Repository/support.quest.com/ ...

  9. DP录 (更新)

    补补弱项 全面发展.. 从最基础来 sdut1299最长上升子序 #include <iostream> #include<cstdio> #include<cstrin ...

  10. ununtu卸载软件

    sudo apt-get remove vim