spring启动(2)--BeanDefinitionRegistry

在spring容器启动的过程中我们和spring中的class都在spring容器中注册成了beanDefinition。那放置这些beanDefinition的就是BeanDefinitionRegistry。Registry的意思是“注册”。spring内部工厂类DefaultListableBeanFactory和 通用上下文类GenericApplicationContext都实现了该接口。

public interface BeanDefinitionRegistry extends AliasRegistry {

/**
   * Register a new bean definition with this registry.
   * Must support RootBeanDefinition and ChildBeanDefinition.
   * @param beanName the name of the bean instance to register
   * @param beanDefinition definition of the bean instance to register
   * @throws BeanDefinitionStoreException if the BeanDefinition is invalid
   * or if there is already a BeanDefinition for the specified bean name
   * (and we are not allowed to override it)
   * @see RootBeanDefinition
   * @see ChildBeanDefinition
   *///使用该注册中心注册一个新的bean definition 必须支持RootBeanDefinition和ChildBean//Definition。
//

void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException;

/**
   * Remove the BeanDefinition for the given name.
   * @param beanName the name of the bean instance to register
   * @throws NoSuchBeanDefinitionException if there is no such bean definition
   */
void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

/**
   * Return the BeanDefinition for the given bean name.
   * @param beanName name of the bean to find a definition for
   * @return the BeanDefinition for the given name (never {@code null})
   * @throws NoSuchBeanDefinitionException if there is no such bean definition
   */
BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;

/**
   * Check if this registry contains a bean definition with the given name.
   * @param beanName the name of the bean to look for
   * @return if this registry contains a bean definition with the given name
   */
boolean containsBeanDefinition(String beanName);

/**
   * Return the names of all beans defined in this registry.
   * @return the names of all beans defined in this registry,
   * or an empty array if none defined
   */
String[] getBeanDefinitionNames();

/**
   * Return the number of beans defined in the registry.
   * @return the number of beans defined in the registry
   */
int getBeanDefinitionCount();

/**
   * Determine whether the given bean name is already in use within this registry,
   * i.e. whether there is a local bean or alias registered under this name.
   * @param beanName the name to check
   * @return whether the given bean name is already in use
   */
boolean isBeanNameInUse(String beanName);

}通过该方法提供的各个方法名称我们能够猜想到他能够获得并操作指定类中的所有bean。类DefaultListableBeanFactory实现了该接口。该工厂实现了

void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)将加载进来的class放置到了容器中beanDefinitionMap(多线程安全)。放置完毕之后会调用方法resetBeanDefinition(beanName)重新设置beanName的所有缓存定义,包括继承它的类。p

rotected void resetBeanDefinition(String beanName) {
// Remove the merged bean definition for the given bean, if already created.//将名称beanName从mergedBeanDefinitions中清除。
clearMergedBeanDefinition(beanName);


//清空实例//1this.factoryBeanInstanceCache.remove(beanName);
//2disposableBeans
//3dependentBeanMap//4、allBeanNamesByType
//5、singletonBeanNamesByType
destroySingleton(beanName);

// Remove any assumptions about by-type mappings.//清除所有by-type映射。
//1、allBeanNamesByType
//2、singletonBeanNamesByTypeclearByTypeCache();

// 清空其父类的缓存依赖。
for (String bdName : this.beanDefinitionNames) {
if (!beanName.equals(bdName)) {
        BeanDefinition bd = this.beanDefinitionMap.get(bdName);
if (beanName.equals(bd.getParentName())) {
           resetBeanDefinition(bdName);
        }
     }
  }
}


转载于:https://my.oschina.net/zjItLife/blog/533280

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值