Spring Basic Concept--di, IOC

Source: Internet
Author: User

I. IOC inversion of control

 PackageCom.qunar.studyspring.bean;ImportCom.qunar.studyspring.dao.PersonDao;ImportCom.qunar.studyspring.object.Person;/*** This is an example of a no control reversal *@authorLiqiu **/ Public classPersonservicebean {PrivatePersondao Persondao =NewPersondao ();  Public voidSave (person person) {Persondao.save (person); }}

Before explaining what a control reversal is, let's start with an example before this technique is presented. As shown above, Persondao is a direct reference, that is, where I want to use the Persondao, where to initialize it, like a crap yes, it doesn't matter, look at the following example of inversion of control:

 PackageCom.qunar.studyspring.bean;ImportCom.qunar.studyspring.dao.PersonDao;ImportCom.qunar.studyspring.object.Person;/*** This is an example of a no control reversal *@authorLiqiu **/ Public classPersonservicebean {PrivatePersondao Persondao; //we can inject the object into the application within the construction method (or the Set method), that is, the Persondao inside the application is created by the external container.     PublicPersonservicebean (Persondao Persondao) { This. Persondao =Persondao; }         Public voidSave (person person) {Persondao.save (person); }}

The difference is in the Personservicebean, when the creation of Persondao, I do not know, there is a program outside the call it. So the so-called control reversal is to give the dependent object to the external easily responsible for creation.

Ii. DI Dependency Injection

The so-called dependency injection is: During runtime, dependent objects are injected dynamically into the component by an external container.

So what's the difference between dependency injection and control inversion? Referring to the network above: "Dependency injection and control inversion are different descriptions of the same thing, except that they describe different angles." Dependency Injection is a description from the perspective of the application, that is, the application relies on the container to create and inject the external resources it needs, whereas control inversion is described from the container's perspective, both: container-controlled applications, and the external resources required by the container to inject applications into the application. ”

Third, the benefits of using spring:

1. Decoupling: Control, service, and DAO are not used in direct declaration with each other

2, things Management Services, JMS services, persistence and so on

3, support single case mode

4, support AOP technology, facing the development of slice

5, there are a lot of auxiliary classes: JdbcTemplate, hibernatetemplate

6. Support the integration of mainstream framework: Hibernate, struts, etc.

Iv. the difference between a lightweight framework and a heavy-weight framework

The main difference is: How much of the service is enabled.

Is spring a heavy-weight framework? If you just use spring's default service, then it's a lightweight framework, which is a heavyweight framework if other services are open to use.

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.