設計模式(三十)----綜合應用-自定義Spring框架-自定義Spring IOC-定義bean、登錄檔相關類

|舊市拾荒|發表於2023-03-30

現要對下面的配置檔案進行解析,並自定義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]);
    }
}

 

 

相關文章