Spring IOC control inversion and di dependency injection

Source: Internet
Author: User

Doc address for Spring framework: https://docs.spring.io/spring/docs/current/spring-framework-reference/

The spring framework, as an excellent open source framework, is undoubtedly highly scalable and highly available. This article records the control reversal of the spring framework.

1. What is control inversion: in system development we are creating an instance. A common practice is to create a new object (as well as other methods of creating instances, eg reflection, Factory mode), which is called positive control by the programmer to create instances manually. Spring's inversion of control is to let the programmer do the work to the spring container to handle. Create an instance to the spring container. This is called control inversion.

2.IOC (total inversion of control) and DI (Dependency injection): Look, this is two names, one control inversion, one dependency injection. Spring's official website describes this:

This chapter covers the Spring Framework implementation of the inversion of Control (IoC) [1] principle. IoC is also known as Dependency Injection (DI). It's a process whereby objects define their dependencies, that's, the other objects they work with, only through Constru ctor arguments, arguments to a factory method, or properties, "set on the" object instance after it is constructed O R returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name inversion of Control (IoC), of the bean itself controlling the I Nstantiation or location of it dependencies by using direct construction of classes, or a mechanism such as the Service L Ocator pattern.

The IOC is also known as DI, which is the process by which an object defines its attribute relationship. We pass in the name of the instance that needs to be constructed, the property to the Spring object factory, the factory method will create the bean instance, and the process of creating it depends on the property passed in. What the programmer does is declare some attributes, and create the instance and give it to the spring container. The spring container uses the factory model (it is recommended to look at the Zen of design Patterns), the programmer inputs the material, and spring produces the product.

What does the 3.spring IOC container do? The IOC container instantiates, assembles, and manages the beans. Instantiation naturally needless to say, assembling is a dependency of the bean and other beans, such as a car class, which contains the Engin (engine) class, Tyre (Tyre) class, etc., which are dependent on the other classes and the class. Management: The scope and lifecycle of the bean is managed, the default bean is a stateless bean, and the request, session, application, prototype, and so on.

4. There are three types of configuration beans that are hosted by spring: One in XML configuration, one for annotations, and one for properties files (almost useless).

5. How the bean container is instantiated in the app: Classpathxmlapplicationcontext and Filesystemxmlapplicationcontext, These two constructors use only the passed-in XML configuration path (and, of course, other constructors)

6. Focus: The Bean's life cycle is divided into a variety of scenarios, here is a scenario: a single case (Bean-singleton) is injected with multiple prototypes (Bean-prototype): A singleton is generated when the container is initialized, only generated once, when the multiple prototypes are injected into a single case , many examples will be instantiated when the singleton is instantiated, when accessing the singleton, multiple cases will not become a stateful bean, he followed the singleton as a stateless bean. This is easy to understand, whether it is a single case or a number of cases are only instantiated once, showing that the behavior of the singleton.

Official website: However, suppose you want the singleton-scoped beans to acquire a new instance of the prototype-scoped Bean repeatedly a T runtime. You cannot dependency-inject a prototype-scoped beans into your singleton beans, because that injection occurs onlyonce< /c0>, when the Spring container was instantiating the singleton bean and resolving and injecting its dependencies. If You need a new instance of a prototype beans at runtime more than once, see Method injection

If you are sure to do this: spring provides the following implementations

This article describes the concept of the IOC and DI in spring, and the definition of the bean, which will explain how the bean is instantiated.

Spring IOC control inversion and di dependency injection

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.