The previous blog gave a brief introduction to spring (I). I will give you a brief introduction to spring-related concepts. Spring is a lightweight control inversion (IOC) and cross-section (AOP) container framework created to solve the complexity of enterprise application development. There are two key points in this sentence: IOC and AOP. Today we will talk about the first IOC.
IOC Concept
Inversion of control is an important Object-Oriented Programming Law to reduce the coupling problem of computer programs. It also has a name called dependency injection ). IOC is not a technology. It is a design model.
Instance demo
To better illustrate IOC, I would like to give you a simple example: A company has set up a new project team with several members and a project leader, at the first meeting after the establishment of the project team, Xiao Li, as the project leader, first gave a brief introduction to himself.
Based on the above description, if we write the following code and class diagram:
Public class Li {public void introduce () {system. out. println ("Hello everyone, I am Xiao Li") ;}} public class team {public void firstmeeting () {Li = new Li (); Li. introduce ();}}
The specific class diagram is as follows:
The above code should have basically completed the relevant requirements, but after careful consideration, we will find that the above Code is based on the specific scenario description and is not abstracted, in this way, we cannot flexibly arrange the project leader to start the course. That is, according to the current Code, the opening self-introduction is bound to Xiao Li and cannot be arranged for others. To solve the above problem, we introduce the leader interface first. The related code and class diagram are as follows:
Public interface leader {public void introduce ();} public class Li implements leader {@ overridepublic void introduce () {system. out. println ("Hello everyone, I am Xiao Li") ;}} public class team {public void firstmeeting () {leader Li = new Li (); Li. introduce ();}}
The specific class diagram is as follows:
Although the above Code can be scheduled to open to other members, we can see that the team class depends on the leader interface and the Li class at the same time, it does not achieve the goal that our expected team only depends on the leader interface. How can this problem be solved? Of course, the boss is introduced, and the boss decides who is the project leader. The specific class diagram and Code are as follows:
Public interface leader {public void introduce ();} public class Li implements leader {@ overridepublic void introduce () {system. out. println ("Hello everyone, I am Xiao Li") ;}} public class team {public void firstmetting (leader) {leader. introduce () ;}} public class boss {public void direct () {leader = new Li (); team = new team (); team. firstmetting (leader );}}
The specific class diagram is as follows:
Through the code and illustration above, we can see that by introducing the boss class, we can decouple the project team and who is the project leader.
Corresponding to the above example, we will explain the IOC, which is literally divided into control and reversal. In the above example, we will control who is the project leader, the reversal is to decide who is the project leader and transfer it to the boss class.In general, the control of the specific implementation class (LI) of the interface is separated from the call class (Team) and transferred to a third party (boss) for decision.
So far, we have already finished the concept of IOC. How can we implement it in spring? This will be explained in detail for specific spring instances later. You only need to know the concepts and implementation ideas of IOC.