Implementation of Spring source parsing-IOC Container

Source: Internet
Author: User

What is a 1.IOC container?

  IOC (inversion of control) controlled inversion: the dependency between objects that would have been managed by the application is now given to the container management, which is called control inversion, which is given to the IOC container, and spring's IOC container is mainly implemented using DI. No active lookups are required, and object lookups, positioning, and creation are all managed by the container.

The object is not created in the program. We used to call a method of an object, first to new object. However, using an IOC container, the code does not directly connect to the object, but rather describes which object to use in the configuration file. The container is responsible for linking these together.

The basic functional specifications of the IOC container are defined by beanfactory, meaning that beanfactory defines the most basic form of the IOC container and provides the most basic service guidelines that IOC containers should adhere to. It is also the most basic specification for our use of IOC containers. In spring code, Beanfactory is just an excuse class, and does not give a concrete implementation of the container, we can see a variety of concrete implementations, such as Xmlbeanfactory,applicationcontext. Here is the code for Beanfactory: (explained below)

Package org.springframework.beans.factory;
Import org.springframework.beans.BeansException;
Public interface Beanfactory {

Here we can get the Factorybean itself by escaping the character "&", used to differentiate the objects produced by the Factorybean through the container and obtain the Factorybean itself.
String Factory_bean_prefix = "&";


Get the corresponding bean through the bean name
Object Getbean (String name) throws Beansexception;

/**

* Get the corresponding bean, by the name of the bean and the type of the owning
*/
<T> T Getbean (String name, Class<t> requiredtype) throws beansexception;

/**

* Get the appropriate bean by the type of the owning
*/
<T> T Getbean (class<t> requiredtype) throws beansexception;

/**
Get the bean through the bean name and the appropriate type of retrieval requirements
*/
Object Getbean (String name, Object ... args) throws beansexception;

/**
Determine if a bean is included
*/
Boolean Containsbean (String name);

/**
To query whether the specified bean is a singleton type bean for the corresponding property can be specified in Beandefinition
*/
Boolean Issingleton (String name) throws Nosuchbeandefinitionexception;

/**
To query whether the specified bean is a prototype type bean for the corresponding property can be specified in Beandefinition
*/
Boolean isprototype (String name) throws Nosuchbeandefinitionexception;

/**
The class type of the bean that queries the specified name is not a specific class type class type that allows the user to define
*/
Boolean Istypematch (String name, Class targetType) throws nosuchbeandefinitionexception;

/**
Query the class type of the bean with the specified name
*/
Class<?> GetType (String name) throws Nosuchbeandefinitionexception;

/**
The query specifies all aliases for the name Bean. These aliases are defined in the Beandefinition
*/
String[] Getaliases (String name);

}

How the 2.IOC container xmlbeanfactory works

This figure is the corresponding xmlbeanfactory structure diagram

Xmlbeanfactory only provides the functionality of the most basic IOC container, which can read beandefinition defined in XML form. How does the xmlbeanfactory implement XML reads? The processing of these XML file definition information is not handled directly by Xmlbeanfactory. In Xmlbeanfactory, a Xmlbeandefinitionreader object is initialized, and Xmlbeandefinitionreader is Xmlbeanfactory can be defined as XML beandefinition, so the processing of XML is actually handled by Xmlbeandefinitionreader. To build this xmlbeanfactory need to specify the source of Beandefinition data, see below the source code should be very clear, there is spring resource class to give. Resource is the class of the spring encapsulated IO operation. As in the following example:

Classpathresource resource = new Classpathresource ("Applicationcontext.xml"), so that the specific Classpathresource class constructs the corresponding resource, It is then passed as a construction parameter to Xmlbeanfactory, so that the corresponding bean can be defined by Beandefinition to initialize the container and the process of dependency injection.

The source code of Xmlbeanfactory is as follows:

Package org.springframework.beans.factory.xml;
Import org.springframework.beans.BeansException;
Import org.springframework.beans.factory.BeanFactory;
Import org.springframework.beans.factory.support.DefaultListableBeanFactory;
Import Org.springframework.core.io.Resource;

//This class inherits the Defaultlistablebeanfactory, which contains the important functions of the IOC container and is used in many places.
public class Xmlbeanfactory extends Defaultlistablebeanfactory {
    private final Xmlbeandefinitionreader reader = new Xmlbeandefinitionreader (this);
    /**
     Pass in a resource to load beandefinition
     */in XML form definition
    public xmlbeanfactory (Resource Resource) throws beansexception {
     & nbsp  this (resource, NULL);
    }

    /**
     Pass in a resource to load the beandefinition   defined in XML form and its dependent Beanfactory object for loading
     */
    public xmlbeanfactory (Resource Resource, Beanfactory parentbeanfactory) throws beansexception {
        super ( Parentbeanfactory);
        this.reader.loadbeandefinitions (Resource);
    }

}
Use example:

Classpathresource resource = new Classpathresource ("Applicationcontext.xml");

Defaultlistablebeanfactory factory = new Defaultlistablebeanfactory ();

Xmlbeandefinitionreader read = new Xmlbeandefinitionreader (factory);

Read.loadbeandefinitions (Resource);

This allows me to use the defaultlistablebeanfactory as an IOC container through the factory object. The corresponding steps:

1. Create an abstract resource for the IOC configuration file that contains the definition of the corresponding bean.

2. Create a Beanfactory

3. Create a reader to load the beandefinition and configure it to beanfactory with a callback

4. After reading the configuration information from a defined resource location and completing the entire onboarding and registering of the corresponding bean definition, the IOC container is ready to use.

Implementation of Spring source parsing-IOC Container

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.