我们把 Bean 的创建交给容器,而不是我们在调用时候传递一个实例化好的 Bean 对象,另外还需要考虑单例对象,在对象的二次获取时是可以从内存中获取对象的。此外不仅要实现功能还需要完善基础容器框架的类结构体,否则将来就很难扩容进去其他的功能了。
1.创建Bean定义信息
public class BeanDefinition {
private Class beanClass;
public BeanDefinition(Class bean){
this.beanClass = bean;
}
public Class getBeanClass() {
return beanClass;
}
public void setBeanClass(Class beanClass) {
this.beanClass = beanClass;
}
}
2.单实例获取接口
public interface SingletonBeanRegistry {
Object getSingleton(String beanName);
}
3.单实例获取接口实现类
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
private Map<String, Object> singletonObjects = new HashMap<>();
@Override
public Object getSingleton(String beanName) {
return singletonObjects.get(beanName);
}
protected void addSingleton(String beanName, Object singletonObject) {
singletonObjects.put(beanName, singletonObject);
}
}
4.Bean工厂获取bean
public interface BeanFactory {
public Object getBean(String name);
}
5.Bean信息注册
public interface BeanDefinitionRegistry {
/**
* 向注册表中注册 BeanDefinition
*
* @param beanName
* @param beanDefinition
*/
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
}
5.抽象获取Bean工厂
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
@Override
public Object getBean(String name) {
Object singleton = getSingleton(name);
if (singleton == null){
BeanDefinition beanDefinition = getBeanDefinition(name);
return createBean(name, beanDefinition);
}
return singleton;
}
protected abstract Object createBean(String name, BeanDefinition beanDefinition);
protected abstract BeanDefinition getBeanDefinition(String name);
}
6.抽象创建Bean工厂
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory{
@Override
protected Object createBean(String name, BeanDefinition beanDefinition) {
Object bean = null;
try {
bean = beanDefinition.getBeanClass().newInstance();
addSingleton(name,bean);
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
}
7.默认Bean注册实现类
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
private Map<String, Object> singletonObjects = new HashMap<>();
@Override
public Object getSingleton(String beanName) {
return singletonObjects.get(beanName);
}
protected void addSingleton(String beanName, Object singletonObject) {
singletonObjects.put(beanName, singletonObject);
}
}
8.默认Bean工厂实现类
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry{
private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap();
@Override
protected BeanDefinition getBeanDefinition(String beanName) {
BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
if (beanDefinition == null) {
System.out.println("No bean named '" + beanName + "' is defined");
}
return beanDefinition;
}
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
beanDefinitionMap.put(beanName,beanDefinition);
}
}
9.定义异常信息
public class BeansException extends RuntimeException {
public BeansException(String msg) {
super(msg);
}
public BeansException(String msg, Throwable cause) {
super(msg, cause);
}
}