Bean instantiation (4) post-processing

Example:

Defining classes that implement the FactoryBean interface:

Test results:

Phenomenon:

  1. Instead of getting an instance of myFactoryBean through “myFactoryBean”, you get a Student instance.
  2. Get the myFactoryBean instance with “&myFactoryBean”.
  3. After the Spring container is loaded, MyFactoryBean is instantiated, but Student is not instantiated until fetch.

The source code parsing

Line 877: The bean is instantiated to determine whether the current bean is implementedFactoryBeanInterface.

Line 878: getBean() for FactoryBean;

getBean(FACTORY_BEAN_PREFIX + beanName)

Line 246: Convert beanName. Call to this class method as follows:

1. Conversion beanName

The ampersand of “&beanName” is removed and beanName is returned.

2. Obtain it from level-1 cache

Line 250: Get the instance from level 1 cache first.

Prerequisites: The context object has been initialized and the instance of MyFactoryBean exists in the level-1 cache. BeanName is the converted name, without an am&, and you get the MyFactoryBean instance.

  1. applicationContext.getBean(“myFactoryBean”); You can get an instance of MyFactoryBean. SharedInstance is an instance of MyFactoryBean.
  2. applicationContext.getBean(“&myFactoryBean”); You can get the instance, sharedInstance is also an instance of MyFactoryBean.

3. getObjectForBeanInstance(sharedInstance, name, beanName, null)

BeanName must not have an ampersand (‘ & ‘).

  1. applicationContext.getBean(“myFactoryBean”); SharedInstance is an instance of MyFactoryBean. Name = “MyFactoryBean”, beanName = “MyFactoryBean”, MBD = NULL
  2. applicationContext.getBean(“&myFactoryBean”); SharedInstance is an instance of MyFactoryBean, name = “& MyFactoryBean “, beanName = “MyFactoryBean”, MBD = null

Line 1792: Check if the current bean is an” & “, as shown in the figure below. If so, the current bean is passed “&myFactoryBean”.

Line 1796: Check again to see if the current bean implements the FactoryBean interface, and throw an exception if it does not, because current is the logic of the FactoryBean.

Line 1802: Return MyFactoryBean instance directly.

Conclusion: line 1792-1803: applicationContext) getBean (” & myFactoryBean “) to the if judgment, finally return from level 1 cache access to myFactoryBean instance


Method, is applicationContext) getBean (” myFactoryBean “) logic.

Line 1808-1810: Returns if the beanInstance (which was just fetched from the level-1 cache) is not of type FactoryBean. Line 1817: Get the instance from the factoryBeanObjectCache.

Note: The cache at this point is not the level 1 cache of the Spring container. The factoryBeanObjectCache cache is an instance that stores the return from getObject() of the FactoryBean class.

Line 1827: Create if cache is not available.

Line 169: Call getObject() of the FactoryBean; Create the Student object and add it tofactoryBeanObjectCacheThe cache.

Conclusion:

  1. ApplicationContext. GetBean (” “), the “& factoryBean factoryBean instance,” preach “factoryBean” obtain getObject () returns the object.
  2. The FactoryBean class is instantiated with the creation of the IOC container, but the objects in getObject() are not instantiated until they are acquired.
  3. Objects in getObject() are also created by IOC, but are not managed in a level 1 cache, they arefactoryBeanObjectCacheThe cache.

Note: this article through the source code + line description of the way to describe, if not understand can leave a message. This article is only a personal learning record, there are mistakes, please correct.