SPRING-IOC source code Interpretation of 2.3-beandefinition registration

Source: Internet
Author: User
Tags assert

In Defaultlistablebeanfactory, a hashmap holds the loaded beandefinition information, the HASHMAP definition can be seen in defaultlistablebeanfactory:

Private final map<string, beandefinition> beandefinitionmap = new concurrenthashmap<string, BeanDefinition > ();

The process of registering the parsed beandefinition to the IOC container Beandefinitionmap is done after the load beandefinition completes. Defaultlistablebeanfactory implements the Beandefinitionregistry interface, this registration process is to set the parsed beandefinition to HashMap, One thing to be aware of is the beandefinition of the same name, which needs to be handled according to the allowbeandefinitionoverding configuration.

public void Registerbeandefinition (String beanname, Beandefinition beandefinition) throws        beandefinitionstoreexception {assert.hastext (beanname, "Bean name must not being empty");        Assert.notnull (beandefinition, "beandefinition must not being null"); if (beandefinition instanceof abstractbeandefinition) {try {(abstractbeandefinition) Beandefi            nition). Validate (); } catch (Beandefinitionvalidationexception ex) {throw new Beandefinitionstoreexception (Beandefi            Nition.getresourcedescription (), Beanname, "Validation of bean definition failed", ex); }}//registration process requires synchronization to ensure data consistency synchronized (THIS.BEANDEFINITIONMAP) {//check whether a bean with the same name has been registered, if it exists and does not allow            Overwrite throws exception Object oldbeandefinition = This.beanDefinitionMap.get (beanname); if (oldbeandefinition! = null) {if (!this.allowbeandefinitionoverriding) {throw new be AndefinitionstoreexceptIon (Beandefinition.getresourcedescription (), Beanname, "Cannot register bean definition [" + beandefinition + "] for Bean                ' + Beanname + ': There is already ["+ Oldbeandefinition +"] bound. "); else {if (this.logger.isInfoEnabled ()) {this.logger.info ("overriding Bean Defi                    Nition for Beans ' "+ beanname +" ': replacing ["+ Oldbeandefinition +"] with ["+ Beandefinition +"] "); }}} else {//normal registration process, put the name of the bean into Beandefinitionname, and Beanname as                Map's key,beandefinition as the value of map This.beanDefinitionNames.add (beanname);            This.frozenbeandefinitionnames = null;            } this.beanDefinitionMap.put (Beanname, beandefinition);        Resetbeandefinition (Beanname); }    }

  

Completing the Beandefinition registration completes the IOC container initialization process, where the entire bean configuration information has been established in defaultlistablebeanfactory, and these beandefinition can already be used by the container. The purpose of the container is to process and maintain this information, which is the basis for the container to establish a dependency reversal.

SPRING-IOC source code Interpretation of 2.3-beandefinition registration

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.