IOC and DI

Source: Internet
Author: User

IoC control reversal
  

 Public class personservicebean{    privatenew  Persondao ();      Public void Save (person person) {    persondao.save (person);    }}

As shown in the code above, Persondaobean is created and maintained within the application. The so-called inversion of Control (IoC) is that the application itself is not responsible for the creation and maintenance of dependent objects, the creation and maintenance of dependent objects is the responsibility of the external container, so that control is transferred from the application to the external container, the transfer of control is called inversion. Control can understand the management (creation and destruction) of the beans needed to run the application, and if you look at the code as an application, the bean that the application needs to run is Persondaobean.

DI Dependency Injection

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 using constructor parameters, let the container put the created dependent object     // injected into the Personservicebean can also be injected using setter. public      personservicebean (Persondao persondao) {        this. Persondao = Persondao;    }      Public void Save (person person) {        persondao.save (person);    }}

Dependency Injection: At run time, dependent objects are dynamically injected into the component by an external container.

  

IOC and Di

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.