脚本宝典收集整理的这篇文章主要介绍了ApplicationContext是如何被注入的,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
//ERROR No qualifying bean of tyPE 'org.sPRingframework.context.ApplicationContext' available applicationContext.getBean(ApplicationContext.class); //SUCCESS @component public class SimpleBean3 { @Autowired private ApplicationContext applicationContext; @Autowired private SimpleBean2 simpleBean2; }
ApplicationContext是Spring中的重要组件,它不是bean,因此无法通过getBean获取它,但是可以通过Autowired注入获得,其中必定有特殊的处理。
普通Bean的元数据存放在DefaultListableBeanFactory的beanDefinITionnames和beanDefinitionMap,普通Bean通过遵照Spring提供的机制自动注册添加,这是Spring提供的功能。
private volatile List<String> beanDefinitionNames = new ArrayList<>(256); private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
ApplicationContext和BeanFactory存储在DefaultListableBeanFactory的resolvableDependencies,它们需要手动注册添加,这是Spring的框架内部逻辑
private final Map<Class<?>, Object> resolvableDependencies = new ConcurrentHashMap<>(16);
在查找依赖时,会同时搜寻beanDefinitionNames和resolvableDependencies,因此ApplicationContext也能被查找到。而getBean时只会查找上面的BeanDefinitionMap,因此找不到ApplicationContext。
注入流程注册 ApplicationContext 为 resolvableDependencies在 AbstractApplicationContext.prepareBeanFactory() 中, ApplicationContext 被注册到 resolvableDependencies 中。
protected void prepareBeanFactory(@R_777_1058@urableListableBeanFactory beanFactory) { //...忽略部分代码 // BeanFactory interface not registered as resolvable type in a plain factory. // MessageSource registered (and found for autowiring) as a bean. beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); beanFactory.registerResolvableDependency(ResourceLoader.class, this); beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); beanFactory.registerResolvableDependency(ApplicationContext.class, this); //...忽略部分代码 }
生成Bean时查找依赖
REF
ApplicationContext是如何被注入的
https://www.jianshu.COM/p/64a25883b836
@Autowired可以注入ApplicationContext
https://zhuanlan.zhihu.com/p/124249445
以上是脚本宝典为你收集整理的ApplicationContext是如何被注入的全部内容,希望文章能够帮你解决ApplicationContext是如何被注入的所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。