The overall structure of the Spring IOC source code specifically explained

Source: Internet
Author: User

The overall structure of Spring ICO's specific interpretation

IOC Introduction

IOC, the core of spring. Always throughout spring. Intuitive to say. It is the spring that is responsible for controlling the life cycle of objects and the relationships between objects, abstracting the relationships between objects. The spring container controls the timing of object generation and reduces the coupling between objects.

Open Spring IOC source code learning

The main source code for SPRINGIOC is the Spring-beans and Spring-context two packages. In the previous article, we talked about how to compile the spring source code and then import Maven project into eclipse.

I. Beanfactory in the spring IOC

In spring, the main IOC container interface is beanfactory. This interface provides the most important function for the implementation of the detailed IOC container, such as the following class diagram, which embodies the beanfactory architecture




The beanfactory, as the topmost interface class, defines the basic functional specifications of the IOC container.

  • Beanfactory has three subcategories: Listablebeanfactory, Hierarchicalbeanfactory, and Autowirecapablebeanfactory.

    But from what we can find finally, the default implementation class is Defaultlistablebeanfactory. He implements all the interfaces.

    So why define so many levels of interfaces? Consult the source code and description of these interfaces found. Every interface has his use of the occasion, it is mainly in order to distinguish within the Spring in the process of object transfer and transformation process. Restrictions on the data access of the object.

    For example, the Listablebeanfactory interface indicates that these beans are available for listing. Hierarchicalbeanfactory, however, indicates that these beans are inherited, that is, each bean may have a parent bean. The Autowirecapablebeanfactory interface defines the Bean's own active assembly rules.

    These four interfaces collectively define the collection of beans, the relationships between beans, and the bean behavior.

    Let's take a look at Beanfactory source code

    public  interface   Beanfactory  { //here is an escape definition for Factorybean, given the assumption that using be The name of the  an retrieves Factorybean objects that are generated by the factory,//assuming that the factory itself is needed. You need to escape string factory_bean_prefix =  "&"; This is based on the Bean's name. Get the bean instance in the IOC container, the IOC container is a large abstract factory. 

    Object Getbean (String name) throws Beansexception; The bean instance is given here based on the Bean's name and class type, unlike the method above, which throws an exception: Suppose the class type of the bean instance obtained by name and the required difference.

    Object getBean(String name, Class requiredType) throws BeansException; //这里提供对bean的检索。看看是否在IOC容器有这个名字的bean boolean containsBean(String name); //这里依据bean名字得到bean实例。并同一时候推断这个bean是不是单件 boolean isSingleton(String name) throws NoSuchBeanDefinitionException; //这里对得到bean实例的Class类型 Class getType(String name) throws NoSuchBeanDefinitionException; //这里得到bean的别名。假设依据别名检索,那么其原名也会被检索出来 String[] getAliases(String name);}

    Ii. beandefinition in the Spring IOC

    The SPRINGIOC container manages the various bean objects we define and their relationship to each other. The Bean object is described by beandefinition in the spring implementation, and its inheritance system such as the following class diagram

    Third, spring IOC analysis of Beandefinitionreader

    The parsing process of the Bean is very complex, the function is very thin, because there are so many places to be expanded, there must be enough flexibility to deal with the possible changes. The parsing of the Bean is mainly the parsing of the Spring configuration file.

    This parsing process is done primarily through the classes in:


    In the middle, xmlbeandefinitionreader and Defaultbeandefinitiondocumentreader have a relationship, Because there is a defaultbeandefinitiondocumentreader in Xmlbeandefinitionreader, not XML has an inheritance relationship with default

    Iv. ApplicationContext Architecture in the spring IOC

    ApplicationContext Series containers are perhaps the most familiar to us, as the xmlwebapplicationcontext used in Web projects belong to this inheritance system, as well as classpathxmlapplicationcontext. The inheritance system, for example, is as seen in:

    The top four categories are the most core class diagrams in spring IOC. By clearing the four class diagrams, we have a basic understanding of the entire ICO usage process. The following will analyze the IOC container initialization source code

  • The overall structure of the Spring IOC source code specifically explained

    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.