从BeanDefinition到Bean实例
对比SimpleBeanDefinitionRegistry
和DefaultListableBeanFactory
的源码,中间省略一些无关紧要的逻辑:
SimpleBeanDefinitionRegistry.java
public class SimpleBeanDefinitionRegistry extends SimpleAliasRegistry implements BeanDefinitionRegistry {
/** Map of bean definition objects, keyed by bean name. */
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(64);
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
this.beanDefinitionMap.put(beanName, beanDefinition);
}
@Override
public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
BeanDefinition bd = this.beanDefinitionMap.get(beanName);
if (bd == null) {
throw new NoSuchBeanDefinitionException(beanName);
}
return bd;
}
}
DefaultListableBeanFactory.java
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
/** Map of bean definition objects, keyed by bean name. */
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
// 实现比较复杂暂时省略
}
@Override
public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
// 实现比较复杂暂时省略
}
@Override
public Object getBean(String name) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(name);
}
@Override
public <T> T getBean(Class<T> requiredType) throws BeansException {
return getBean(requiredType, (Object[]) null);
}
}
从类关系上来看DefaultListableBeanFactory
不单单实现了BeanDefinitionRegistry
,还实现了BeanFactory
(继承了AbstractAutowireCapableBeanFactory
)提供了获取bean实例的方法。分析其继承关系如下:文章来源:https://www.toymoban.com/news/detail-827030.html
其中BeanFactory
是IOC容器的核心文章来源地址https://www.toymoban.com/news/detail-827030.html
到了这里,关于Spring源码笔记之SpringIOC--(2)从BeanDefinition到Bean实例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!