Spring温故知新(二) IoC控制反转与DI依赖注入(修正1)
学习Spring,所有教程都是从IoC和DI开始的,但是大部分教程对它们的介绍都很抽象,要是之前没系统的学习过设计模式的话理解起来就非常吃力。所以在这里我尽我的能力来解释这两个概念。
首先的重点,IoC并不是Spring特有的,它是一种设计模式,事实上只要掌握了原理,自己也可以写出一个IoC的实现方法。
一、DI(Dependency Injection)依赖注入可以称为IoC(Inversion of Control)控制反转,但是IoC不等于就是DI
这一点是很多初学者很容易产生错误的一个概念,因为很多教程都把DI和IoC放在一起说,就像堆和堆栈一样,容易让初学者以为他们就是同一个东西,这个是需要纠正的。
简单的解释,就是IoC包括了DI,但是IoC还包括了另外一个叫做DL(Dependency Lookup)依赖查找的功能。你可以先简单的把IoC当作是某种操作,DI是这种操作的写入方式,而DL是查找方式,这样可能容易区分一点。
DL之所以很少有人提起,是因为我们平时很少用到,所以也就慢慢被遗忘了,但是这也不代表它就不存在了。可惜我自己也对DL没有去花时间了解,只是知道有这么个东西,所以在这暂时也没法去详细介绍了,如果有高手的话请指点一下!
二、什么叫做IoC(Inversion of Control)控制反转
这一段是纯理论的介绍,从别的地方直接抄过来的,先看一遍,不懂也没关系,下面我会举例来说明:
package com.iteye.bolide74.action;public class Robot {public String name;public String color;public double height;public double width;public double weight;public Robot(String name, String color, double height, double width,double weight) {this.name = name;this.color = color;this.height = height;this.width = width;this.weight = weight;}public void Speak(String msg) {System.out.println(msg);}}
实现类:
package com.iteye.bolide74.tester;import com.iteye.bolide74.action.Robot;public class IoCTester {public static void main(String[] args) {Robot robot0 = new Robot("robot0", "black", 80.000, 40.0000, 1000.0000);robot0.Speak("Hello,World!");}}package com.iteye.bolide74.tester;import com.iteye.bolide74.action.Robot;class RobotFactory {public Robot getRobot(int robotType) { Robot robot;switch (robotType) {case 0:robot = new Robot("robot0", "black", 80.000, 40.0000, 1000.0000);break;case 1:robot = new Robot("robot1", "white", 90.000, 30.0000, 800.0000);break;default:robot = new Robot("defaultRobot", "red", 10.000, 10.0000, 300.0000);break;}return robot;}}public class IoCTester {public static void main(String[] args) {RobotFactory robotFactory = new RobotFactory();Robot robot = robotFactory.getRobot(0);robot.Speak("Hello,World! My name is " + robot.name);}}package com.iteye.bolide74.action;public class Robot2 {public String name;public String color;public double height;public double width;public double weight;private Robot2(String name, String color, double height, double width,double weight) {this.name = name;this.color = color;this.height = height;this.width = width;this.weight = weight;}public static Robot2 getRobot(int robotType) {Robot2 robot2;switch (robotType) {case 0:robot2 = new Robot2("robot0", "black", 80.000, 40.0000, 1000.0000);break;case 1:robot2 = new Robot2("robot1", "white", 90.000, 30.0000, 800.0000);break;default:robot2 = new Robot2("defaultRobot", "red", 10.000, 10.0000, 300.0000);break;}return robot2;}public void Speak(String msg) {System.out.println(msg + ",我是" + this.name);}}package com.iteye.bolide74.tester;import com.iteye.bolide74.action.Robot2;public class IoCTester {public static void main(String[] args) {Robot2 robot2 = Robot2.getRobot(1);robot2.Speak("Hello,world!");}}package com.iteye.bolide74.impl;public interface ISpeaker {public void Speak(String msg);}package com.iteye.bolide74.action;import com.iteye.bolide74.impl.ISpeaker;public class Robot implements ISpeaker {public String name;public String color;public double height;public double width;public double weight;public Robot(String name, String color, double height, double width,double weight) {this.name = name;this.color = color;this.height = height;this.width = width;this.weight = weight;}@Overridepublic void Speak(String msg) {System.out.println(msg + ",我是" + this.name + ",我的体重为" + this.weight);}}package com.iteye.bolide74.action;import com.iteye.bolide74.impl.ISpeaker;public class People implements ISpeaker {public String name;public double height;public double weight;public People(String name, double height, double weight) {this.name = name;this.height = height;this.weight = weight;}@Overridepublic void Speak(String msg) {System.out.println(msg + ",我是" + this.name + ",我就不告诉你我有多重!");}}package com.iteye.bolide74.tester;import com.iteye.bolide74.action.People;import com.iteye.bolide74.action.Robot;import com.iteye.bolide74.impl.ISpeaker;class SpeakerFactory {public ISpeaker getSpeaker(int speakerType) {ISpeaker speaker;switch (speakerType) {case 0:speaker = new Robot("robot0", "black", 80.000, 40.0000, 1000.0000);break;case 1:speaker = new People("GirlFriend", 1.64, 99.99);break;default:speaker = new Robot("defaultRobot", "red", 10.000, 10.0000,300.0000);break;}return speaker;}}public class IoCTester {public static void main(String[] args) {SpeakerFactory speakerFactory = new SpeakerFactory();ISpeaker speaker = speakerFactory.getSpeaker(1);speaker.Speak("Hello,World!");}}