Spring's inversion of control (IOC) and Dependency Injection (DI) detailed

Source: Internet
Author: User

Spring's inversion of control (IOC) and Dependency Injection (DI) detailed

The following (IOC) control inversion is introduced first:

The so-called control inversion is that the application itself is not responsible for the creation and maintenance of dependent objects, and the creation and maintenance of dependent objects is the responsibility of the external container. So that the controller has been transferred to the external container, the transfer of control is reversed
The sample code is as follows:

public class personservicebean{       private Persondao Persondao = new <strong><span style= "color: #FF0000;" >PersonDaoImpl</span></strong> ();       public void Save (person person) {Persondao.save (person);}      }

Persondaoimpl is created and maintained internally within the app. The creation of an object is externally responsible. This is an IOC.



(DI) Dependency Injection



Dependency Injection is: During a program run, there is an external container that dynamically injects dependent objects into the component.
When we give the dependent object to the external container for creation, the Personservicebean class can be changed to the following:

public class personservicebean{    private Persondao Persondao;    By constructor parameter injection    //can also be injected via setter method public        Personservicebean (Persondao persondao) {        This.persondao = Persondao;    }    public void Save (person person) {        persondao.save (person);}    }

Di is generated on the basis of the IOC.




Spring's inversion of control (IOC) and Dependency Injection (DI) 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.