1. conceptual understanding
- Dependency Injection
- Who injection? Dependent objects injection IoC container .
- Inversion of Control
- Who controls? What control? IoC container control objects , the control creates injected dependent objects .
- Why is it called reverse? Create, target injection control over the subjective wishes of the programmer into IoC container unified management.
Build dependencies between objects 2. traditional way
public class DvdPlayMissionImpossible { private MissionImpossibleCd missionImpossibleCd; public DvdPlayMissionImpossible(){ this.missionImpossibleCd = new MissionImpossibleCd(); // 紧耦合 } public void play(){ System.out.println("一台只能看" + missionImpossibleCd.getCompactDiskName() + "的DVD"); } } public class DvdPlayFurious { private FuriousCd furiousCd; public DvdPlayFurious(){ this.furiousCd = new FuriousCd(); // 紧耦合 } Public void Play () { System.out.println ( "one can only see" + furiousCD.getCompactDiskName () + "a DVD" ); } } public class MissionImpossibleCd { public String getCompactDiskName () { return "Mission Impossible " ; } } public class FuriousCd { public String getCompactDiskName () { return " speed and passion " ; } } // Traditional Way. @Test public void dvdPlayerMissionImpossibleTest () { DvdPlayMissionImpossible dvdPlayMissionImpossible = new newDvdPlayMissionImpossible (); DvdPlayFurious dvdPlayFurious = new new DvdPlayFurious (); dvdPlayMissionImpossible.play (); // one can only see the Mission Impossible DVD dvdPlayFurious.play (); // one can only see the speed and passion of DVD }
Build dependencies between objects 3. DI way
- All have CD movie name, so abstracted into a CompactDisk interface.
public interface CompactDisk { String getCDName(); } @Component public class MissionImpossible implements CompactDisk { @Override public String getCDName() { return "碟中谍"; } } @Component public class Furious implements CompactDisk{ @Override public String getCDName() { return "速度与激情"; } }
- All DVD players have features to play, so abstracted into a MediaPlayer interface.
public interface MediaPlayer { void play(); } @Component public class DvdPlayer implements MediaPlayer { @Autowired @Qualifier("missionImpossible") private CompactDisk cd; @Override public void play() { System.out.println("一台可以看" + cd.getCDName() + "的DVD"); } } @Component public class VCDPlayer implements MediaPlayer { @Autowired @Qualifier("furious") Private CompactDisk CD; @Override public void Play () { System.out.println ( "one can see the" + cd.getCDName () + "the VCD" ); } }
- unit test
@Autowired DVDPlayer DVDPlayer; @Autowired VCDPlayer VCDPlayer; // DI Way. @Test public void dvdAndVcdPlayerTest () { dvdPlayer.play (); // one can see the Mission Impossible DVD vcdPlayer.play (); // a VCD can see the speed and passion }
4. DI advantage
- Decoupling between objects , after all, one can only see a movie player, I do not think people are willing to buy it.
- The same object, IoC containers only need to create one.
- IoC container will help you automatically match the complex dependencies between objects.
5. References
- https://www.iteye.com/blog/jinnianshilongnian-1413846
- https://www.zhihu.com/question/23277575/answer/169698662
- The second chapter "Spring real"