对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. 深入理解Oracle的imp/exp 和各版本之间的规则

    Oracle数据中IMP/EXP工具可用于对数据进行迁移.IMP命令用于把Dmp文件从本地导入到远程数据库服务器,而EXP命令则是把数据从远程数据库服务器导出到本地的Dmp文件.其功能相当于Oracl ...

  2. Linux-ubuntu

    在VMware安装ubuntu时磁盘分配如下: /boot 100MB / 2G swap 512MB /home 5G /var 1G /usr 5G+. 设置root权限:sudo passwd ...

  3. java单例模式使用及注意事项

    1. 说明 1)单例模式:确保一个类只有一个实例,自行实例化并向系统提供这个实例 2)单例模式分类:饿单例模式(类加载时实例化一个对象给自己的引用),懒单例模式(调用取得实例的方法如getInstan ...

  4. Automotive Security的一些资料和心得(7):AUTOSAR和Security

    1. 密码模块[1] 密码模块在Services Layer Configurable and common access to 密码子程序 硬件支持密码模块 2. 应用 应用和密码子程序分离 Cry ...

  5. NUTCH Exception in thread "Thread-12751" java.lang.OutOfMemoryError: PermGen space

    转载自 :http://greemranqq.iteye.com/blog/1705867转载自:http://www.cnblogs.com/xwdreamer/archive/2011/11/21 ...

  6. 怎么修改tomcat默认访问首页

    一般情况下安装好tomcat之后我们的默认访问首页是index了,但我们如果要修改或增加一个默认首页,我们可参考下面办法来解决. 通过 ip:port 访问到的是 tomcat 的管理页面,其他常规部 ...

  7. free 命令解释

    free 命令 buffers and cached 解释 N多人总是询问,当在linux在输入free时内存总数怎么加起来不一样啊,下面我来解释一下free命令的输出. 我们运行free命令时都会看 ...

  8. IPv6 tutorial – Part 8: Special addresses

    https://4sysops.com/archives/ipv6-tutorial-part-8-special-addresses/ The special IPv6 addresses disc ...

  9. BZOJ_1778_[Usaco2010_Hol]_Dotp_驱逐猪猡_(期望动态规划+高斯消元+矩阵)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1778 炸弹从1出发,有\(\frac{P}{Q}\)的概率爆炸,如果不爆炸,等概率移动到连通的 ...

  10. Azure 媒体服务支持 DASH 实时传送流

    Kilroy Hughes Azure媒体服务数字媒体架构师 本文重点介绍 Azure 媒体服务支持的 DASH 实时传送流功能,同时阐述如何利用这些功能将实时和点播自适应流传送至 Web 浏览器 ...