在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