我们把 Bean 的创建交给容器,而不是我们在调用时候传递一个实例化好的 Bean 对象,另外还需要考虑单例对象,在对象的二次获取时是可以从内存中获取对象的。此外不仅要实现功能还需要完善基础容器框架的类结构体,否则将来就很难扩容进去其他的功能了。
微信图片_20220321144656.png

1.创建Bean定义信息

  1. public class BeanDefinition {
  2. private Class beanClass;
  3. public BeanDefinition(Class bean){
  4. this.beanClass = bean;
  5. }
  6. public Class getBeanClass() {
  7. return beanClass;
  8. }
  9. public void setBeanClass(Class beanClass) {
  10. this.beanClass = beanClass;
  11. }
  12. }

2.单实例获取接口

  1. public interface SingletonBeanRegistry {
  2. Object getSingleton(String beanName);
  3. }

3.单实例获取接口实现类

  1. public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
  2. private Map<String, Object> singletonObjects = new HashMap<>();
  3. @Override
  4. public Object getSingleton(String beanName) {
  5. return singletonObjects.get(beanName);
  6. }
  7. protected void addSingleton(String beanName, Object singletonObject) {
  8. singletonObjects.put(beanName, singletonObject);
  9. }
  10. }

4.Bean工厂获取bean

  1. public interface BeanFactory {
  2. public Object getBean(String name);
  3. }

5.Bean信息注册

  1. public interface BeanDefinitionRegistry {
  2. /**
  3. * 向注册表中注册 BeanDefinition
  4. *
  5. * @param beanName
  6. * @param beanDefinition
  7. */
  8. void registerBeanDefinition(String beanName, BeanDefinition beanDefinition);
  9. }

5.抽象获取Bean工厂

  1. public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
  2. @Override
  3. public Object getBean(String name) {
  4. Object singleton = getSingleton(name);
  5. if (singleton == null){
  6. BeanDefinition beanDefinition = getBeanDefinition(name);
  7. return createBean(name, beanDefinition);
  8. }
  9. return singleton;
  10. }
  11. protected abstract Object createBean(String name, BeanDefinition beanDefinition);
  12. protected abstract BeanDefinition getBeanDefinition(String name);
  13. }

6.抽象创建Bean工厂

  1. public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory{
  2. @Override
  3. protected Object createBean(String name, BeanDefinition beanDefinition) {
  4. Object bean = null;
  5. try {
  6. bean = beanDefinition.getBeanClass().newInstance();
  7. addSingleton(name,bean);
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }
  11. return bean;
  12. }
  13. }

7.默认Bean注册实现类

  1. public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {
  2. private Map<String, Object> singletonObjects = new HashMap<>();
  3. @Override
  4. public Object getSingleton(String beanName) {
  5. return singletonObjects.get(beanName);
  6. }
  7. protected void addSingleton(String beanName, Object singletonObject) {
  8. singletonObjects.put(beanName, singletonObject);
  9. }
  10. }

8.默认Bean工厂实现类

  1. public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry{
  2. private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap();
  3. @Override
  4. protected BeanDefinition getBeanDefinition(String beanName) {
  5. BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
  6. if (beanDefinition == null) {
  7. System.out.println("No bean named '" + beanName + "' is defined");
  8. }
  9. return beanDefinition;
  10. }
  11. @Override
  12. public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
  13. beanDefinitionMap.put(beanName,beanDefinition);
  14. }
  15. }

9.定义异常信息

  1. public class BeansException extends RuntimeException {
  2. public BeansException(String msg) {
  3. super(msg);
  4. }
  5. public BeansException(String msg, Throwable cause) {
  6. super(msg, cause);
  7. }
  8. }