实现简单Spring基于XML的配置程序

定义一个容器,使用ConcurrentHashMap 做为单例对象的容器

  1. 先解析beans.xml
  2. 得到第一个bean对象的信息,id,class,属性和属性值
  3. 使用反射生成对象,并赋值
  4. 将创建好的bean对象放入到singletonObjects集合中
  5. 提供getBean(id)方法可以返回对应的bean对象

monster bean

public class Monster {
    private Integer id;
    private String name;
    private String skill;

    public Monster() {
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getSkill() {
        return skill;
    }

    public void setSkill(String skill) {
        this.skill = skill;
    }

    @Override
    public String toString() {
        return "Monster{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", skill='" + skill + '\'' +
                '}';
    }
}

package com.sparrow.spring.application;

import com.sparrow.spring.bean.Monster;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: 诉衷情の麻雀
 * @Description: TODO
 * @DateTime: 2023/7/24 8:09
 **/
public class SpaApplicationContext {
    private ConcurrentHashMap<String, Object> singletonObjects = new ConcurrentHashMap<>();


    //构造器 接收一个容器的配置文件
    public SpaApplicationContext(String iocBeanXmlFile) throws Exception {
        //1. 得到类加载路径
        String path = this.getClass().getResource("/").getPath();

        //2.创建SaxReader
        SAXReader saxReader = new SAXReader();

        //3.得到Document对象
        Document document = saxReader.read(new File(path + iocBeanXmlFile));

        //4.得到rootElement
        Element rootElement = document.getRootElement();

        List<Element> elements = rootElement.elements("bean");
        for (Element bean : elements) {
            String id = bean.attributeValue("id");
            String classFullPath = bean.attributeValue("class");
            Integer monsterId = null;
            String name = "";
            String skill = "";
            //遍历bean下面的property属性
            List<Element> property = bean.elements("property");
            for (Element elementProperty : property) {
                //如果是id把值存起来
                if ("id".equalsIgnoreCase(elementProperty.attributeValue("name"))) {
                    monsterId = Integer.valueOf(elementProperty.attributeValue("value"));
                } else if ("name".equalsIgnoreCase(elementProperty.attributeValue("name"))) {
                    name = elementProperty.attributeValue("value");
                } else if ("skill".equalsIgnoreCase(elementProperty.attributeValue("name"))) {
                    skill = elementProperty.attributeValue("value");
                }

                //利用反射 根据类的全路径 进行实例化
                Class<?> aClass = Class.forName(classFullPath);
                Monster o = (Monster) aClass.newInstance();
                o.setId(monsterId);
                o.setName(name);
                o.setSkill(skill);
                
                //放入容器中
                singletonObjects.put(id, o);
            }


        }
    }

    public Object getBean(String id) {
        return singletonObjects.get(id);
    }
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="com.sparrow.spring.bean.Monster" id="monster01">
        <property name="id" value="1"/>
        <property name="name" value="白骨精"/>
        <property name="skill" value="吃唐僧"/>
    </bean>
    <bean class="com.sparrow.spring.bean.Monster" id="monster02">
        <property name="id" value="2"/>
        <property name="name" value="老鼠精"/>
        <property name="skill" value="吸血"/>
    </bean>

</beans>
public class Test {
    public static void main(String[] args) throws Exception {
        SpaApplicationContext spaApplicationContext = new SpaApplicationContext("beans.xml");
        Monster monster02 = (Monster) spaApplicationContext.getBean("monster02");
        Monster monster01 = (Monster) spaApplicationContext.getBean("monster01");
        System.out.println(monster02);
        System.out.println(monster01);
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诉衷情の麻雀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值