在spring容器启动的过程当中咱们和spring中的class都在spring容器中注册成了beanDefinition。那放置这些beanDefinition的就是BeanDefinitionRegistry。Registry的意思是“注册”。spring内部工厂类DefaultListableBeanFactory和 通用上下文类GenericApplicationContext都实现了该接口。spring
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//四、allBeanNamesByType
//五、singletonBeanNamesByType
destroySingleton(beanName);
// Remove any assumptions about by-type mappings.//清除全部by-type映射。
//一、allBeanNamesByType
//二、singletonBeanNamesByTypeclearByTypeCache();
// 清空其父类的缓存依赖。
for (String bdName : this.beanDefinitionNames) {
if (!beanName.equals(bdName)) {
BeanDefinition bd = this.beanDefinitionMap.get(bdName);
if (beanName.equals(bd.getParentName())) {
resetBeanDefinition(bdName);
}
}
}
}app