Spring源码笔记之SpringIOC--(2)从BeanDefinition到Bean实例
【摘要】 从BeanDefinition到Bean实例对比SimpleBeanDefinitionRegistry和DefaultListableBeanFactory的源码,中间省略一些无关紧要的逻辑:SimpleBeanDefinitionRegistry.javapublic class SimpleBeanDefinitionRegistry extends SimpleAliasRegis...
从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实例的方法。分析其继承关系如下:
其中BeanFactory
是IOC容器的核心
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)