Spring's understanding of the IOC control reversal

Source: Internet
Author: User
Tags configuration settings connection pooling constructor xmlns
Introduction to Spring IOCBeans and context packages are the basis of the IOC container, and Beanfactory provides advanced configuration mechanisms that make it possible to manage various objects. ApplicationContext is an extension of beanfactory, and features are further enhanced, such as easier integration with spring AOP, resource processing (international processing), event delivery, and context implementations of various application tiers ( such as the Webapplicationcontext for Web apps). In short, the actual application more will choose ApplicationContext, because ApplicationContext is inherited the function of beanfactory, while adding more support for enterprise core content.
Spring IOC Key concept understanding

IOC (Inversion of control): controlled inversion, that is, the way to get the object is reversed, we only need to configure the metadata in the Applicationcontext.xml (Spring configuration file) file (that is, the XML) The Bean in the beanfactory specifies the full path of the corresponding class (class), and the container (*. *) in spring is responsible for creating, configuring, and managing the objects and the dependencies between them, and these objects are not different from our objects through new object (). This will eventually achieve the purpose of decoupling (that is, to reduce the dependency between the code);

Bean: In spring, the body that makes up your application (backbone) and the objects managed by the spring IOC container are called beans. In short, the bean is the object that is initialized, assembled, and managed by the spring container, except that the bean is no different from the other objects in the application. The bean definition and the dependencies between the beans are described by configuration metadata.

Container: Refers to the spring IOC container, Beanfactory is the actual representative of the container, he is responsible for the creation of Bean objects, configuring and accommodating the created bean description and management bean;spring provides us with many easy-to-use beanfactory implementations, Xmlbeanfactory is the most common one. The implementation will XML describe the objects that comprise the application and the dependencies between the objects. The Xmlbeanfactory class obtains this XML configuration metadata and uses it to build a fully configurable system or application.


Spring IOC Container

Metadata: The content of the spring configuration XML file that we typically configure is metadata;

<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns= "Http://www.springframework.org/schema/beans"
       xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemalocation= "http// Www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">

  <bean id= "..." class= "..." > <!--Collaborators and configuration for the This bean go to-and
  </ bean>

  <bean id= "..." class= "..." >
    <!--Collaborators and configuration for this bean go to here-->
  </bean>

  <!--more bean definitions Go-here--

</beans>
Injection dependency:

The rationale behind Dependency Injection (DI) is that dependencies between objects (that is, other objects that work together) are implemented only in the following ways: arguments to constructors, parameters of factory methods, or setting properties on objects created by constructors or factory methods. Therefore, the container's job is to inject those dependencies when the bean is created. Control is fundamentally reversed, in contrast to the 3 autonomous control dependency injection methods that are controlled by the bean itself, by specifying dependencies directly in the constructor, or similar to the service locator (Locator) pattern, which is also the control reversal (inversion of  Control, IoC) The origin of the name. Spring's IOC key configuration 1. How to define (create) a bean

The Spring IOC container will manage one or more beans that will be created through the bean definitions in the configuration file (in XML format as <bean/> elements).

Inside the container, these bean definitions are represented by the Beandefinition object, which contains the following information:

Fully qualified class name: This is usually the actual implementation class of the defined bean.

The definition of bean behavior, which determines the behavior of the bean in the container (scope, life cycle callback, and so on)

References to other beans can also be referred to as collaboration Beans (collaborators) or dependent beans (dependencies).

Additional configuration settings when you create a bean instance. For example, using beans to define connection pooling, you can specify the number of connections through attributes or construction parameters, and connection pool size limits.

The above content is translated directly into a set of properties contained in each bean definition. The specific properties attribute is referenced below: Spring official translation document;

2.bean How to Instantiate

Essentially, a bean definition describes how to create one or more object instances. When needed, the container takes a specified bean definition from the bean definition list and uses the reflection mechanism to create (or obtain) an actual object based on the configuration metadata inside the bean definition.

When the metadata is configured with XML descriptions, the type of the instantiated object is specified through the class property of the <bean/> element. The class attribute (which corresponds to the class attribute of the Beandefinition instance) is usually necessary (although there are two exceptions, see section 3.2.3.2.3, "Instantiate with instance factory method" and section 3.6, "Inheritance of bean definition"). There are two main uses of the class attribute: In most cases, the container will invoke the constructor of the specified class directly through reflection to create the bean (which is somewhat similar to using the new operator in Java code); In rare cases, the container invokes the static factory method of the class to create the bean instance, class Property will be used to specify a class that actually has a static factory method (the object type created by the call to the static factory method is the current class or other classes does not matter). There are three main ways of instantiation: Instantiate with a constructor, instantiate with a static method, instantiate with instance factory;

3.di-injection Dependency (meaning that the dependencies between objects and objects are established by configuration) are implemented in the following ways: parameters of constructors, parameters of factory methods, or setting properties for objects created by constructors or factory methods. Di mainly has two kinds of injection methods, namely setter injection and constructor injection;

3.1 (setter) is first a setter di example defined in XML format. The associated XML configuration is as follows:

<bean id= "Examplebean" class= "examples. Examplebean ">

  <!--setter injection using the nested <ref/> element--
  <property name=" Beanone "><ref bean=" Anotherexamplebean "/></property>

  <!--setter injection using the neater ' Ref ' attribute--
  <property name= "Beantwo" ref= "Yetanotherbean"/>
  <property name= " Integerproperty "value=" 1 "/>
</bean>

<bean id=" Anotherexamplebean "class=" examples. Anotherbean "/>
<bean id=" Yetanotherbean "class=" examples. Yetanotherbean "/>
public class Examplebean {

    private anotherbean beanone;
    Private Yetanotherbean Beantwo;
    private int i;

    public void Setbeanone (Anotherbean beanone) {
        this.beanone = Beanone;
    }

    public void Setbeantwo (Yetanotherbean beantwo) {
        this.beantwo = beantwo;
    }

    public void Setintegerproperty (int i) {
        this.i = i;
    }    
}
3.2 (how constructor parameters are) as you can see, the setter method in the Bean class corresponds to one by one of the properties configured in the XML file. Next is the example of constructor injection:
<bean id= "Examplebean" class= "examples. Examplebean >

  <!--constructor injection using the nested <ref/> element--
  < constructor-arg>
    <ref bean= "Anotherexamplebean"/>
  </constructor-arg>
  
  <!-- constructor injection using the neater ' ref ' attribute--
  <constructor-arg ref= ' Yetanotherbean '/>
  
  <constructor-arg type= "int" value= "1"/>
</bean>

<bean id= "Anotherexamplebean" class= " Examples. Anotherbean "/>
<bean id=" Yetanotherbean "class=" examples. Yetanotherbean "/>
public class Examplebean {

    private anotherbean beanone;
    Private Yetanotherbean Beantwo;
    private int i;
    
    Public Examplebean (
        anotherbean Anotherbean, Yetanotherbean yetanotherbean, int i) {
        This.beanone = Anotherbean ;
        This.beantwo = Yetanotherbean;
        this.i = i;
    }
}
4.Bean life cycle Callbacks We may do some initialization or freeing of resources before the bean is created or destroyed;

Spring provides several flag interfaces (marker interface) that are used to alter the behavior of beans in a container, including Initializingbean and Disposablebean. The bean that implements both interfaces will invoke the Afterpropertiesset () method of the former and the Destroy () method of the latter at initialization and destruction.

Spring uses the Beanpostprocessor implementation internally to handle any flag interfaces it can find and invoke the appropriate method. If you need custom features or life cycle behavior, you can implement your own beanpostprocessor. More on this can be seen in section 3.7, "Container extension point".

These are just some of the key content records that I've learned from the official Spring translation documents, please refer to the official translation documentation for specific studies.

Reference:

Spring official translation Document: Http://shouce.jb51.net/spring/beans.html


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.