BeanDefinitionRegistry is responsible for registration of BeanDifinition, where:

/** * Register a new BeanDefinition instance in the registry */
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
      throws BeanDefinitionStoreException;
Copy the code
/** * Removes the BeanDefinition instance */ that it registered in the registry
void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
Copy the code
/** * Get the specified BeanDefinition instance */ from the registry
BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
Copy the code
/** * Check whether the BeanDefinition instance is in the registry */
boolean containsBeanDefinition(String beanName);
Copy the code

My previous article in Spring column wrote, DefaultListableBeanFactory inherited BeanDefinitionRegistryAnd it also has abeanDefinitionMap

/** Map of bean definition objects, keyed by bean name. */
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);
Copy the code

So registration is storing the corresponding <key,value> in a beanDefinitionMap


DefaultBeanDefinitionDocumentReader againprocessBeanDefinitionmethodsRegistration method is introduced into two parameters, BeanDefinition instances of packaging and Registry instance, getRegistry returns the member variables in the AbstractBeanDefinitionReader:

@Override
public final BeanDefinitionRegistry getRegistry(a) {
   return this.registry;
}
Copy the code

Registry is passed in through the constructor of the class.

And then I’m going to go into the registerBeanDefinition,

public static void registerBeanDefinition( BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
      throws BeanDefinitionStoreException {

   // Register bean definition under primary name.
   // Register the beanDefinition and its name in the container
   String beanName = definitionHolder.getBeanName();
   registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

   // Register aliases for bean name, if any.
   // If there are aliases, register them into the container one by one
   String[] aliases = definitionHolder.getAliases();
   if(aliases ! =null) {
      for(String alias : aliases) { registry.registerAlias(beanName, alias); }}}Copy the code

The beanDefinition and its name are first registered in the container, and then aliases one by one, if any exist.

To enter theregisterBeanDefinitionMethods:Check whether or notAbstractBeanDefinitionInstance, due toGenericBeanDefinitionisAbstractBeanDefinitionInstance, so definitely go into if.

Validate verifies that the lookup and replacemethod override methods exist and are valid.

The next step is to retry, try to get it from the registryBeanDefinitionIf it already exists, the container configuration determines whether to overwrite the original instanceBeanDefinitionIf it is allowed to be overwritten, it will be overwritten after various checksbeanDefinitionMapThe < key, value >If it didn’tBeanDefinitionIf the container has already created a BeanDefinition, it will lock the map and put the instance into the BeanDefinition

// Check if any BeanDefinitions with the same name are already registered in the IOC container
if(existingDefinition ! =null || containsSingleton(beanName)) {
   // Try to reset the cache of all registered Beandefinitions, including BeanDefinitions
   // the parent class of the merged beanDefinition and the cache of merged BeanDefinitions
   // Refers to the beandefinition with the parent attribute, which will put the parent's beandefinition
   // The BeanDefinition attribute is merged together
   resetBeanDefinition(beanName);
}
Copy the code

ResetBeanDefinition is A recursive call process. For example, if A is updated this time, and the parent of B is A, B also needs to be reset. All attributes related to B, including B’s original merged properties, Bean singletons, and Bean post-processor processing, also need to be reset. After B changes, we should check whether B is the parent, and also do the corresponding operation……