Spring源码解析之Bean在IOC容器中的注册(三)
??????bdHolder.getBeanName() + "'", ele, ex);
???}
???// Send registration event.
???getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
??}
?}
真正的注册发生在BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry()); 中,这里注册器其实就是之前一直提到的底层工厂DefaultListableBeanFactory(实现了BeanDefinitionRegistry).
public static void registerBeanDefinition(
???BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
???throws BeanDefinitionStoreException {
??// Register bean definition under primary name.
??String beanName = definitionHolder.getBeanName();
??registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
??// Register aliases for bean name, if any.
??String[] aliases = definitionHolder.getAliases();
??if (aliases != null) {
???for (String aliase : aliases) {
????registry.registerAlias(beanName, aliase);
???}
??}
?}
下面我们来看一下registerBeanDefinition的源码(BeanDefinitionReaderUtils)
public static void registerBeanDefinition(
???BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
???throws BeanDefinitionStoreException {
??// Register bean definition under primary name.
??String beanName = definitionHolder.getBeanName();
??registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
??// Register aliases for bean name, if any.
??String[] aliases = definitionHolder.getAliases();
??if (aliases != null) {
???for (String aliase : aliases) {
????registry.registerAlias(beanName, aliase);
???}
??}
?}
我们可以看到真正的注册发生在registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());这一步。继续往里跟,我们来看一下DefaultListableBeanFactory是怎么实现的
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
???throws BeanDefinitionStoreException {
??Assert.hasText(beanName, "Bean name must not be empty");
??Assert.notNull(beanDefinition, "BeanDefinition must not be null");
??if (beanDefinition instanceof AbstractBeanDefinition) {
???try {
????((AbstractBeanDefinition) beanDefinition).validate();
???}
???catch (BeanDefinitionValidationException ex) {
????throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
??????"Validation of bean definition failed", ex);
???}
??}
??synchronized (this.beanDefinitionMap) {
???Object oldBeanDefinition = this.beanDefinitionMap.get(beanName);
???if (oldBeanDefinition != null) {
????if (!this.allowBeanDefinitionOverriding) {
?????throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
???????"Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
???????"': There is already [" + oldBeanDefinition + "] bound.");
????}
????else {
?????if (this.logger.isInfoEnabled()) {
??????this.logger.info("Overriding bean definition for bean '" + beanName +
????????"': replacing [" + oldBeanDefinition + "] with [" + beanDefinition + "]");
?????}
????}
???}
???else {
????this.beanDefinitionNames.add(beanName);
????this.frozenBeanDefinitionNames = null;
???}
???this.beanDefinitionMap.put(beanName, beanDefinition);
???resetBeanDefinition(beanName);
??}
?}
我们看到其实底层存储这个BeanDefinition的数据结构就是一个简单的Map
/** Map of bean definition objects, keyed by bean name */
?private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();
?