模拟spring依赖注入

  上一篇模拟spring扫描注册bean对象 https://blog.csdn.net/qq_28852755/article/details/108716365

1.依赖注入可能会造成循环依赖 解决办法为构造2个map

                           a) 1个是完整的bean对象

                           b)1个是早期暴露出的bean对象

    private ConcurrentHashMap<String, Object> singletonObjects = new ConcurrentHashMap<>();
    private ConcurrentHashMap<String, Object> earlySingletonObjects  = new ConcurrentHashMap<>();

2.解析A类中的@Autowired注解的属性

首先通过beanclass无参构造生成对象 放入到earlySingletonObjects中,然后对beanclass中得有autowired注解的属性进行注入(默认通过beanname进行注入)。 在属性注入的时候通过getbean去获取bean对象在singletonObjects 对象没有的时候再从earlySingletonObjects去获取bean对象完成属性对象的获取,以上就完成对bean循环依赖注入。

 private Object createBean(String beanName, BeanDefinition beanDefinition) {
        Class beanClass = beanDefinition.getBeanClass();
        Object newInstance = null;
        try {
            // 无参构造 生成对象
             newInstance = beanClass.getDeclaredConstructor().newInstance();
             // 提前放入到 earlySingletonObjects 中
             earlySingletonObjects.put(beanName,newInstance);
             Field[] fields = beanClass.getDeclaredFields();
             for (Field field : fields) {
                if(field.isAnnotationPresent(Autowired.class)){
                    //所以依赖注入的,必须是Component过的
                    Object object = getBean(field.getName());
                    field.setAccessible(true);
                    field.set(newInstance,object);
                }
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return newInstance;
    }
public Object getBean(String beanName){
        Object bean = singletonObjects.get(beanName);
        if(bean == null){
            // 如果 singletonObjects没有才从earlySingletonObjects中获取对象
            bean = earlySingletonObjects.get(beanName);
            if(bean == null){
                BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
                bean = createBean(beanName, beanDefinition);
                singletonObjects.put(beanName,bean);
            }

        }
        return bean;
    }

 

测试如下:

 public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        AService aService = (AService) annotationConfigApplicationContext.getBean("a");
        aService.query();
        Bservice bservice = (Bservice) annotationConfigApplicationContext.getBean("b");
        bservice.query1();
    }

结果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值