Spring IOC Detailed

Source: Internet
Author: User

Spring IOC detailed 1. Control reversal

Control inversion is a way to generate or acquire specific objects by describing (XML or annotations) and by third parties. The implementation of control inversion in spring is the IOC container, which is implemented by dependency injection (Dependency injection, DI).

Its greatest benefit is to reduce the degree of coupling between objects, in a system which classes, how the implementation does not need to understand, just want to know what it is useful. It's just that the object's generation relies on the IOC container, not the developer's active behavior. The active creation of the model, the responsibility of the developer, and in the passive mode, the responsibility attributed to the IOC container, based on such a passive way, it can be said that the object is controlled reversed.

2. Spring IOC Container

The role of the Spring IOC container is to accommodate the various beans that we have developed, and we can get a variety of beans published in the spring IOC container, which can be obtained by description.

The Spring IOC container is designed primarily based on the beanfactory and ApplicationContext two interfaces, where ApplicationContext is one of Beanfactory's sub-interfaces, in other words beanfactory is the underlying interface defined by the spring IOC container, and ApplicationContext is one of its advanced interfaces and has a number of useful extensions to the Beanfactory functionality, so in most cases will use ApplicationContext as the spring IOC container. is the main design of the spring-related IOC container interface.

From this picture, we can see that Beanfactory is located at the bottom of the design, it provides the SPRINGIOC the lowest design, can see its source code.

 Packageorg.springframework.beans.factory;Importorg.springframework.beans.BeansException;Importorg.springframework.beans.factory.NoSuchBeanDefinitionException; Public Interfacebeanfactory {String factory_bean_prefix= "&"; Object Getbean (String var1)throwsbeansexception; <T> T Getbean (String var1, class<t> var2)throwsbeansexception; <T> T Getbean (class<t> var1)throwsbeansexception; Object Getbean (String var1, Object ... var2)throwsbeansexception; BooleanContainsbean (String var1); BooleanIssingleton (String var1)throwsnosuchbeandefinitionexception; BooleanIsprototype (String var1)throwsnosuchbeandefinitionexception; BooleanIstypematch (String var1, class<?> var2)throwsnosuchbeandefinitionexception; Class<?> GetType (String var1)throwsnosuchbeandefinitionexception; String[] Getaliases (String var1);}


The following is a brief analysis of the source code:

1) Several methods of Getbean are used to obtain the bean configured for the spring IOC container. It can be a string from a parameter type, or it can be a class type. Because the class type can extend the interface can also inherit the parent class, so there is a certain extent that the parent class type is unable to obtain an exact instance of the exception, such as the student class, but there are boys and girls two classes, this time through the student class cannot get instances from the container, because the container can not judge the specific implementation class.

2) Issingleton is used to determine whether a singleton, if judged true, means that the bean exists as a single singleton in the container. In contrast, Isprototype, if judged to be true, means that when you get the bean from the container, the container generates a new instance for you. By default, Spring creates a singleton for the bean, or Issingleton returns True by default, and Isprototype returns false,

3) about the match of type, this is a way of matching Java type.

4) Getaliases is the way to get aliases.

Initialization and dependency injection of the 3.Spring IOC container

In learning to understand the initialization and dependency injection of the spring IOC container, we should note that the definition and initialization of the bean is a two step in the spring IOC container, which is defined first, then initialized and dependent injected.

The following is a detailed description of the bean definition. The life cycle of the spring bean is detailed in the next blog post.

The bean definition is divided into 3 steps:

1) Resource positioning, this step is the spring IOC container based on the developer's configuration, resource positioning, in the development of spring, through XML or annotations are very common way, the positioning of the content is provided by the developer.

2) Beandefinition loading, this is just the information that the resource is anchored to, saved in the bean definition (beandefinition), and the bean instance is not created at this time.

3) Beandefinition Registration, this process is to publish the beandefinition information into the spring IOC container, there is still no instance of the corresponding bean to create.

At this point, the bean is defined in the spring IOC container, not initialized, and the dependency injection is not completed, that is, the resource that does not inject its configuration to the bean, it is not fully used. We can use it only after we have completed the initialization and injected it with dependency.

Learn more about the story and listen to tell. A detailed explanation of the life cycle of the spring bean and the initialization and dependency injection of the spring IOC container will be explained in detail in the next blog post.


Spring IOC Detailed

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.