当我们需要动态注入Bean,并给该Bean的属性注入其他Bean时,比如在Mybatis和Spring的整合中,我们需要动态注入Mapper到spring容器中,而该Mapper如果需要执行SQL语句,还需要持有SqlSessionFactory的引用。但是我们注入时,可能对应的Bean还没有准备好,这时,我们就可以使用RuntimeBeanReference,以保持对实际Bean的引用。在Spring处理依赖关系时,最终会将该引用替换成实际生成的Bean对象。例如:
definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
博客介绍了在Spring里动态注入Bean并给其属性注入其他Bean的情况,如Mybatis和Spring整合时动态注入Mapper到容器,若注入时对应Bean未准备好,可使用RuntimeBeanReference保持对实际Bean的引用,Spring处理依赖时会将其替换为实际Bean。
865

被折叠的 条评论
为什么被折叠?



