手写简单的beanFactory主要分两步:
1、定义bean对象,对应Spring中的< bean >标签;
2、创建BeanFactory,根据 BeanFactory 来获取实例对象;
bean定义对象:
package com.lks.util;
/**
* Created by likaisong on 2019/3/3.
*/
public class BeanDefined {
private String beanId;
private String classPath;
public void setClassPath(String classPath) {
this.classPath = classPath;
}
public String getBeanId() {
return beanId;
}
public String getClassPath() {
return classPath;
}
}
创建beanFactory
package com.lks.util;
import java.util.List;
/**
* Created by likaisong on 2019/3/3.
*/
public class BeanFactory {
private List<BeanDefined> beanDefinedList;
public List<BeanDefined> getBeanDefinedList() {
return beanDefinedList;
}
public void setBeanDefinedList(List<BeanDefined> beanDefinedList) {
this.beanDefinedList = beanDefinedList;
}
/**
* 通过反射实例化对象
* @param beanId
* @return
* @throws Exception
*/
public Object getBean(String beanId) throws Exception {
Object instance = null;
for (BeanDefined bean : beanDefinedList){
if (beanId.equals(bean.getBeanId())){
String classPath = bean.getClassPath();
in