Start learning Java Fundamentals and ideas ~
The inversion of Control (IOC) is the core idea of the spring framework, which, in my own words, is that you have to do one thing, don't be yourself new, you say what you want to do, and then outsource it.
Dependency Injection (DI) in my shallow thoughts, it is through the reference of interfaces and the expression of construction methods, that something is done in turn to the place that needs to be used.
It's good to do this: To do a single duty, and to improve reusability, after decoupling, any way you can do it, using the interface's reference call method, never need to change
Lift a chestnut:
Write an interface, say we go shopping ~
Public Interface ishopping { void Drive (); String money (); }
In the realization of it two: the rich people shopping, no money to shop--! This chestnut is lifted
public class RichMan "implements ishopping{@Override public void drive () {System.out.println ( "Drive by Benz" ); } @Override public String money () { System.out.println ( Lot's money " return "10000" ; }}
Public class Implements ishopping{ @Override publicvoid Drive () { System.out.println (" Take a walk! " ); } @Override public String Money () { System.out.println ("nothing"); return "1"; }}
Then we go out to play, by the way shopping a bit ~
Public class Play { private ishopping shoppingsomething;
Using the construction method, the incoming public Play (ishopping shoppingsomething) { this is implemented. shoppingsomething = shoppingsomething; } Public void Withgirlfriend () { shoppingsomething.drive (); Shoppingsomething.money (); }
}
The way you want to use it, inject it in a container, and here's how to inject it under the simulation:
Public class Containner { public Play getshopping () { returnnew Play ( New RichMan ());} }
Test it:
Public Static void Main (string[] args) { new Containner (); = containner.getshopping (); Play.withgirlfriend (); }
The result is:
Drive by Benz
Lot ' s of money
If you rely on the injected configuration again, the implementation of the poor, then we do not have to change the code, the result will change ~ Well, it should be understood.
Understanding of Java control inversion and dependency injection