Spring Bean Creation Process

Source: Internet
Author: User

0. Typically, the first instantiation of the bean's entry, whether it is Dispatcherservlet, Contextloaderlistener, or Classpathxmlapplicationcontext, is not at every call to Getbean , but in the Preinstantiatesingletons method of the Defaultlistablebeanfactory class: When the configuration file is loaded, the non-abstract, non-deferred loading is in advance (lazy-init= False) is initialized through the Getbean method of Abstractbeanfactory to the spring container for an alternate

1. Regardless of which Getbean method of overloading, finally will be attributed to the Dogetbean method of the call

2. First, the Dogetbean method formats the passed-in string name parameter into the beanname used in the spring container, with a two-point format: The first is to remove the prefix from the Spring framework class (such as various beanfactory, etc.). The second is to convert the alias of a Bean that uses alias (alias) to a real name

3. The Single Instance definition object (sharedinstance) of the bean in spring is stored in a concurrenthashmap named Singletonobjects in the spring container. Gets the sharedinstance based on the formatted Beanname (the sharedinstance is being created in this case, which will return an earlier reference to that sharedinstance)

4. Determine whether Sharedinstance exists:

4.1. If sharedinstance exists, use it directly to invoke Getobjectforbeaninstance

4.2.1. If it does not exist, get the bean from the parent beanfactory attempt

4.2.2. If a failure is obtained from the parent beanfactory, the mbd of the Bean's definition object is obtained first through beanname, and then the other beans that the bean is dependent on from the mbd are instantiated.

4.2.2.1. If the bean is defined as singleton in mbd, then the sharedinstance of the Bean is created (the process of actually creating the sharedinstance is actually the Objectfactory method that invokes the Createbean interface. Implemented by the Abstractautowirecapablebeanfactory class)

4.2.2.2. If defined as prototype, create a scopedinstance substitution sharedinstance

4.2.3. After the creation is done it is also called getobjectforbeaninstance into the next step

5.1. The Getobjectforbeaninstance method first determines whether the incoming beaninstance (sharedinstance or scopedinstance) is an indirect reference to a factory class but at the same time it is not a factory class (true throws an exception)

5.2. If it is not a factory class, or an indirect reference to a factory class, return the beaninstance directly

5.3. If the beaninstance is a factory class, the real instance is generated from the factory class and returned

6. If there is a specified class<?> in the earliest incoming parameter, the type cast is performed or the instance is returned directly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.