Spring IOC Container implementation

Source: Internet
Author: User

The two IOC vessels of the 1,spring beanfactory
    • IOC container of the underlying type;
    • Deferred initialization policy (when container initialization is complete, the Bean object is not created and initialized only when the initialization request is received);
    • Because of delayed initialization, the boot speed is faster and the resource is less.
ApplicationContext
    • On the basis of beanfactory, we have added more advanced specific: Event release, internationalization, etc.
    • Complete the creation of all beans when the container is started;
    • Longer start-up time and more resource usage;
2,IOC container main class/interface introduction

1) beanfactory
It is an interface that provides methods for getting the beans in a container.

2) Beandefinitionregistry
It is the container for the IOC, which stores and manages all bean objects.

3) Defaultlistablebeanfactory
It is a concrete implementation of the IOC container , implements the Beanfactory and Beandefinitionregistry interfaces, and therefore has both a container for managing beans and a way to access the beans .

4) Beandefinition
Each bean has a beandefinition that stores information about the bean: The class type of the object, whether it is an abstract class, a constructor method parameter , and so on.

rootbeandefinition and childbeandefinition are the two main implementation classes of Beandefinition.

5) Beandefinitionreader

In spring, there are four ways to label a Bean's dependencies:

      1. Declare directly in the code
      2. Declaring through an XML file
      3. Declaring through the properties file
      4. Declaration by annotations
        The purpose of the Beandefinitionreader interface is to read the bean information in the configuration file, parse it into a Beandefinition object, and register it in Beandefinitionregistry.
        Propertiesbeandefinitionreader and Xmlbeandefinitionreader are two implementation classes for this interface, respectively, to parse the properties and XML-formatted configuration files.

6) Xmlbeanfactory
It is a beanfactory with integrated Xmlbeandefinitionreader functionality to simplify initialization operations.

Two important stages of 3,beanfactory container start-up phase

At this stage, Spring uses Beandefinitionreader to load the configuration file and parses all the beans into the Beandefinition object and registers it with the beandefinitionregistry .

Bean instantiation Phase

For beanfactory containers, if the current bean is not initialized, or if the bean is configured to prototype, when the caller actively calls the Getbean method or implicitly calls Getbean due to the existence of a dependency container, it Triggers initialization of the bean instance .

4,beanfactorypostprocessor: a container extension mechanism

Spring provides the container extension mechanism of beanfactorypostprocessor, which allows us to insert additional operations after the container is started andbefore the bean is instantiated .

The Beanfactorypostprocessor provides three implementation classes:

1.PropertyPlaceholderConfigurer
In general, we do not write database connection information directly to the datasource of this bean, but instead write them separately in a properties file, which is easy to modify and read. Instead, the beans use placeholders instead of these property values, and when the container starts, replace the placeholder with the value in the properties file before the bean is initialized , and then create the object.
Propertyplaceholderconfigurer can achieve this function.

    • The following configuration is made in XML:
<BeanID= "DataSource"class= "xxxxxx">    < Propertyname= "url">        <value>${jdbc.url}</value>    </ Property>    < Propertyname= "username">        <value>${jdbc.username}</value>    </ Property></Bean>
    • To store property values using the properties file:
Jdbc.url=jdbc:mysql://127.0.0.1:3306jdbc.username=root

When the container is started, the DataSource Beandefinition object will be registered in Beandefinitionregistry, and the attribute value in Beandefinition is still the placeholder form; Propertyplaceholderconfigurer will work, and it will replace the placeholder with the attribute value in the properties file . The bean can then be created correctly.

2.PropertyOverrideConfigurer
It functions similarly to propertyplaceholderconfigurer and requires specifying a properties file, except that it replaces the attribute values of the specified bean with the attribute values of those beans set in the configuration file.

    • The following configuration is made in XML:
<BeanID= "DataSource"class= "xxxxxx">    < Propertyname= "url">        <value>jdbc:mysql://127.0.0.1:3306</value>    </ Property>    < Propertyname= "username">        <value>Chai</value>    </ Property></Bean>
    • To store property values using the properties file:
Datasource.url=jsbc:mysql://127.0.0.1:3307datasource.username=root

Propertyoverrideconfigurer replaces the specified property value by modifying the Beandefinition object after the container is started and before the Bean object is created.
The contents of the properties file must follow the following format:

The name of the bean. Property Name = property value

3.CustomEditorConfigurer

This class is used to add custom PropertyEditor objects to the spring container.

Before the container starts, all beans in the configuration file are parsed into the Beandefinition object, and all the information about the bean is string-type to create the Bean object. You need to parse these string types into their original type. In spring, each type has a corresponding PropertyEditor class, which encapsulates the conversion method of string with that type. Of course, for some types spring does not provide the corresponding propertyeditor, we can customize the PropertyEditor, and use Customeditorconfigurer to tell the spring container, Let it use our custom propertyeditor to parse it when it encounters that type.

Some of the propertyeditor provided by spring:

      • Stringarraypropertyeditor
        Converts a string to string[], by default, split.
      • Classeditor
        Similar to Class.forName (string), converts a string into a class object.
      • Fileeditor
        Converts a string into a file object.
      • UrlEditor
        Converts a string into a URL object.
      • Inputstreameditor
        Converts a string into a InputStream object.
      • Localeeditor
        Converts a string into a locale object.
      • Patterneditor
        Converts a string into a pattern object.

These types of strings, spring will automatically convert them to the original type. Our custom propertyeditor must be added to the container via Customeditorconfigurer.

How do I turn on the beanfactorypostprocessor feature?
1.BeanFactory

Create Beanfactory object Configurablelistablebeanfactory beanfactory = new Xmlbeanfactory (new Classpathresource ("xxx"));// Create Beanfactorypostprocessor object Propertyplaceholderconfigurer processor = new Propertyplaceholderconfigurer ();// Set the location of the properties file processor.setlocation ("xxx");//Pass it to Beanfactoryprocessor.postprocessbeanfactory (beanfactory);

2.ApplicationContext
ApplicationContext automatically detects the beanfactorypostprocessor that appear in the configuration file, so you only need to declare the beanfactorypostprocessor used in the configuration file.

<Beanclass= "xxxxx." Propertyplaceholderconfigurer ">    < Property>        <List>            <value>Properties File path</value>        </List>    </ Property></Bean>

55269888

Spring IOC Container implementation

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.