简版SpringIOC实现(自定义SpringIOC)

本文详细介绍了Spring框架中的Bean定义(如POJO类如PropertyValue和BeanDefinition),注册表类(如BeanDefinitionRegistry和SimpleBeanDefinitionRegistry),解析器类(如BeanDefinitionReader和XmlBeanDefinitionReader)以及IoC容器(如ApplicationContext和ClassPathXmlApplicationContext)的实现,重点展示了如何解析XML配置文件进行bean对象的管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 定义bean相关的pojo类:PropertyValue类、MutablePropertyValue类、BeanDefinition类;
  2. 定义注册表相关类:BeanDefinitionRegistry接口、SimpleBeanDefinitionRegistry类;
  3. 定义解析器相关类:BeanDefinitionReader接口、XmlBeanDefinitionReader类;
  4. 定义IOC容器相关类:BeanFactory接口、ApplicationContext接口、AbstractApplicationContext类、ClassPathXmlApplicationContext类;

要对下面的配置文件进行解析,并自定义Spring框架的IOC对涉及的对象进行管理。

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="userDao" class="com.itheima.springframework.component.UserDaoImpl">
        <property name="userName" value="xiaoWang"/>
        <property name="password" value="123456"/>
    </bean>

    <bean id="userService" class="com.itheima.springframework.component.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
</beans>

1. 定义bean相关的pojo类

1.1 PropertyValue类

用于封装bean的属性,体现到上面的配置文件就是封装bean标签的子标签Property标签数据;

package com.itheima.springframework.beans;

/**
 * @author name:    silk
 * @version 1.0
 * @description: 用于封装bean的属性,即封装bean标签的子标签property标签数据
 * @date 2024/3/16 15:13
 */
public class PropertyValue {
   

    private String name;
    private String ref;
    private String value;

    public PropertyValue() {
   
    }

    public PropertyValue(String name, String ref, String value) {
   
        this.name = name;
        this.ref = ref;
        this.value = value;
    }

    public String getName() {
   
        return name;
    }

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

    public String getRef() {
   
        return ref;
    }

    public void setRef(String ref) {
   
        this.ref = ref;
    }

    public String getValue() {
   
        return value;
    }

    public void setValue(String value) {
   
        this.value = value;
    }
}

1.2 MultablePropertyValues类

一个bean标签可以有多个property子标签,所以再定义一个MultablePropertyValues类,用来存储并管理多个PropertyValue对象;

package com.itheima.springframework.beans;

import cn.hutool.core.collection.CollUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author name:    silk
 * @version 1.0
 * @description: 用于存储和管理多个PropertyValue对象
 * @date 2024/3/16 15:26
 */
public class MutablePropertyValues implements Iterable<PropertyValue>{
   

    private final List<PropertyValue> propertyValueList;

    public MutablePropertyValues(List<PropertyValue> propertyValueList) {
   
        if (CollUtil.isNotEmpty(propertyValueList)) {
   
            this.propertyValueList = propertyValueList;
        } else {
   
            this.propertyValueList = new ArrayList<>();
        }
    }

    public MutablePropertyValues() {
   
        this.propertyValueList = new ArrayList<>();
    }

    /***
     * @description: 获取所有的propertyValue对象
     * @author name silk
     * @date: 2024/3/16 15:41
     */
    public PropertyValue[] getPropertyValues() {
   
        return propertyValueList.toArray(new PropertyValue[0]);
    }

    /***
     * @description: 根据property的name属性值获取propertyValue对象
     * @author name silk
     * @date: 2024/3/16 15:52
     */
    public PropertyValue getPropertyValue(String pvName) {
   
        for (PropertyValue propertyValue : propertyValueList) {
   
            if (pvName.equals(propertyValue.getName(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值