设计模式(三十)----综合应用-自定义Spring框架-自定义Spring IOC-定义bean、注册表相关类

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

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
    <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
</beans>

1 定义bean相关的pojo类

1.1 PropertyValue类

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

/**
 * @version v1.0
 * @ClassName: PropertyValue
 * @Description: 用来封装bean标签下的property标签的属性
 *              name属性
 *              ref属性
 *              value属性 : 给基本数据类型及String类型数据赋的值
 */
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 MutablePropertyValues类

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

/**
 * @version v1.0
 * @ClassName: MutablePropertyValues
 * @Description: 用户存储和管理多个PropertyValue对象
 */
public class MutablePropertyValues implements Iterable<PropertyValue> {
​
    //定义list集合对象,用来存储PropertyValue对象
    private final List<PropertyValue> propertyValueList;
​
    public MutablePropertyValues() {
        this.propertyValueList = new ArrayList<PropertyValue>();
    }
​
    public MutablePropertyValues(List<PropertyValue> propertyValueList) {
        this.propertyValueList = (propertyValueList != null ? propertyValueList : new ArrayList<PropertyValue>());
    }
​
    //获取所有的PropertyValue对象,返回以数组的形式
    public PropertyValue[] getPropertyValues() {
        //将集合转换为数组并返回
        return this.propertyValueList.toArray(new PropertyValue[0]);
    }
​
    //根据name属性值获取PropertyValue对象
    public PropertyValue getPropertyValue(String propertyName) {
        //遍历集合对象
        for (PropertyValue pv : this.propertyValueList) {
            if (pv.getName().equals(propertyName)) {
                return pv;
            }
        }
        return null;
    }
​
    //获取迭代器对象
    @Override
    public Iterator<PropertyValue> iterator() {
        return propertyValueList.iterator();
    }
​
    //判断集合是否为空
    public boolean isEmpty() {
        return this.propertyValueList.isEmpty();
    }
​
    //添加PropertyValue对象
    public MutablePropertyValues addPropertyValue(PropertyValue pv) {
        //判断集合中存储的PropertyValue对象是否和传递进行的重复了,如果重复了,进行覆盖
        for (int i = 0; i < this.propertyValueList.size(); i++) {
            //获取集合中每一个PropertyValue对象
            PropertyValue currentPv = this.propertyValueList.get(i);
            if (currentPv.getName().equals(pv.getName())) {
                this.propertyValueList.set(i, new PropertyValue(pv.getName(),pv.getRef(), pv.getValue()));
                return this;//目的就是实现链式编程
            }
        }
        this.propertyValueList.add(pv);
        return this;//目的就是实现链式编程
    }
​
    //判断是否有指定name属性值的对象
    public boolean contains(String propertyName) {
        return getPropertyValue(propertyName) != null;
    }
}

1.3 BeanDefinition类

BeanDefinition类用来封装bean信息的,主要包含id(即bean对象的名称)、class(需要交由spring管理的类的全类名)及子标签property数据。

/**
 * @version v1.0
 * @ClassName: BeanDefinition
 * @Description: 用来封装bean标签数据
 *      id属性
 *      class属性
 *      property子标签的数据
 */
public class BeanDefinition {
    private String id;
    private String className;
​
    private MutablePropertyValues propertyValues;
​
    public BeanDefinition() {
        propertyValues = new MutablePropertyValues();
    }
​
    public String getId() {
        return id;
    }
​
    public void setId(String id) {
        this.id = id;
    }
​
    public String getClassName() {
        return className;
    }
​
    public void setClassName(String className) {
        this.className = className;
    }
​
    public void setPropertyValues(MutablePropertyValues propertyValues) {
        this.propertyValues = propertyValues;
    }
​
    public MutablePropertyValues getPropertyValues() {
        return propertyValues;
    }
}

2 定义注册表相关类

2.1 BeanDefinitionRegistry接口

BeanDefinitionRegistry接口定义了注册表的相关操作,定义如下功能:

  • 注册BeanDefinition对象到注册表中

  • 从注册表中删除指定名称的BeanDefinition对象

  • 根据名称从注册表中获取BeanDefinition对象

  • 判断注册表中是否包含指定名称的BeanDefinition对象

  • 获取注册表中BeanDefinition对象的个数

  • 获取注册表中所有的BeanDefinition的名称

/**
 * @version v1.0
 * @ClassName: BeanDefinitionRegistry
 * @Description: 注册表对象
 */
public interface BeanDefinitionRegistry {
​
    //注册BeanDefinition对象到注册表中
    void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
​
    //从注册表中删除指定名称的BeanDefinition对象
    void removeBeanDefinition(String beanName) throws Exception;
​
    //根据名称从注册表中获取BeanDefinition对象
    BeanDefinition getBeanDefinition(String beanName) throws Exception;
​
    boolean containsBeanDefinition(String beanName);
​
    int getBeanDefinitionCount();
​
    String[] getBeanDefinitionNames();
}

2.2 SimpleBeanDefinitionRegistry类

该类实现了BeanDefinitionRegistry接口,定义了Map集合作为注册表容器。

/**
 * @version v1.0
 * @ClassName: SimpleBeanDefinitionRegistry
 * @Description: 注册表接口的子实现类
 */
public class SimpleBeanDefinitionRegistry implements BeanDefinitionRegistry {
​
    //定义一个容器,用来存储BeanDefinition对象
    private Map<String, BeanDefinition> beanDefinitionMap = new HashMap<String, BeanDefinition>();
​
    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName,beanDefinition);
    }
​
    @Override
    public void removeBeanDefinition(String beanName) throws Exception {
        beanDefinitionMap.remove(beanName);
    }
​
    @Override
    public BeanDefinition getBeanDefinition(String beanName) throws Exception {
        return beanDefinitionMap.get(beanName);
    }
​
    @Override
    public boolean containsBeanDefinition(String beanName) {
        return beanDefinitionMap.containsKey(beanName);
    }
​
    @Override
    public int getBeanDefinitionCount() {
        return beanDefinitionMap.size();
    }
​
    @Override
    public String[] getBeanDefinitionNames() {
        return beanDefinitionMap.keySet().toArray(new String[1]);
    }
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章