- 定义bean相关的pojo类:PropertyValue类、MutablePropertyValue类、BeanDefinition类;
- 定义注册表相关类:BeanDefinitionRegistry接口、SimpleBeanDefinitionRegistry类;
- 定义解析器相关类:BeanDefinitionReader接口、XmlBeanDefinitionReader类;
- 定义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(