Spring IoC 使用详解

fmms 13年前
     <p>在Spring中,依赖注入(DI)模式实现了控制反转(IoC)原理。让我们通过一个例子来帮助理解依赖注入。我们先看到java版的例子,然后在此基础上加上spring的功能。就例子而言,是相当地简单。<span style="font-family:'Courier New';">QuizMater接口暴露了<span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;">popQuestion()方法。为了保持简单性,<span style="font-family:'Courier New';">QuizMaster将只生成一个问题。</span></span></span></p>    <p><span style="font-family:'Courier New';"><span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"> </span></span></p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * QuizMaster.java  */ package com.vaannila;  public interface QuizMaster {   public String popQuestion(); }</pre>    <p><span style="font-family:'Courier New';">StrutsQuizMaster和SpringQuizMaster类实现了QuizMaster接口,它们各自生成struts和spring相关的问题。</span></p>    <p><span style="font-family:'Courier New';"> </span></p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * StrutsQuizMaster.java  */ package com.vaannila;  public class StrutsQuizMaster implements QuizMaster {   @Override  public String popQuestion() {   return "Are you new to Struts?";  }  }</pre>    <p></p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * SpringQuizMaster.java  */ package com.vaannila;  public class SpringQuizMaster implements QuizMaster {   @Override  public String popQuestion() {   return "Are you new to Spring?";  }  }</pre>    <p>有个QuizMasterService类向用户显示问题。QuizMasterService类关联了QuizMaster接口。</p>    <p> </p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * QuizMasterService.java  */ package com.vaannila;  public class QuizMasterService {   private QuizMaster quizMaster = new SpringQuizMaster();   public void askQuestion(){   System.out.println(quizMaster.popQuestion());  } }</pre>    <p>最后创建QuizProgram类来管理测验。</p>    <p> </p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * QuizProgram.java  */ package com.vaannila;  public class QuizProgram {   public static void main(String[] args) {   QuizMasterService quizMasterService = new QuizMasterService();   quizMasterService.askQuestion();  }  }</pre>    <p>看上去相当的简单,我们创建了<span style="font-family:'Courier New';">QuizMasterService类的实例,并且调用了askQuestion()方法。当你执行<span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;">program,期望的“<span style="font-family:'Courier New';">Are you new to Spring?</span>”将从控制台打印出来。</span></span></p>    <p><span style="font-family:'Courier New';"><span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;">再看看这个例子的类图。绿色箭头表示泛化,蓝色箭头表示关联。</span></span></p>    <p><span style="font-family:'Courier New';"><span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"><img title="Spring IoC 使用详解" border="0" alt="Spring IoC 使用详解" src="https://simg.open-open.com/show/5e048ddb50592889fbcbb60da997d09b.gif" width="590" height="353" /></span></span></p>    <p> 看的出来这种结构是紧密耦合在一起的。在<span style="font-family:'Courier New';">QuizMasterService类中创建了QuizMaster的实例,如下所示。</span></p>    <p><span style="font-family:'Courier New';"> </span></p>    <pre class="brush:java; toolbar: true; auto-links: false;">private QuizMaster quizMaster = new SpringQuizMaster();</pre>    <p>为了测验精通struts的人,我们需要修改QuizMasterService成这样:</p>    <p> </p>    <pre class="brush:java; toolbar: true; auto-links: false;">private QuizMaster quizMaster = new StrutsQuizMaster();</pre>    <p>因此耦合性是非常高的,这就是为什么要使用依赖注入来避免这种耦合。Spring框架提供了非常强大的容器来管理组件。容器就是基于控制反转(IoC)理念并且实现了依赖注入。这些组件仅仅需要选择一种接受资源的方式,容器会自动为组件推送资源。</p>    <p>下面我们替换为<span style="font-family:'Courier New';">QuizMasterService类直接创建QuizMaster对象的方式,让容器来承担这项工作。替换了硬编码,允许容器来注入需要的依赖项。</span></p>    <p><span style="font-family:'Courier New';">注入依赖使用<span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;">setter或构造方法注入。下面看看怎样使用<span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;">setter来注入。</span></span></span></p>    <p><span style="font-family:'Courier New';"><span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"><span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"> </span></span></span></p>    <pre class="brush:java; toolbar: true; auto-links: false;">/**  * QuizMasterService.java  */ package com.vaannila;  public class QuizMasterService {   private QuizMaster quizMaster;    public void setQuizMaster(QuizMaster quizMaster) {   this.quizMaster = quizMaster;  }   public void askQuestion(){   System.out.println(quizMaster.popQuestion());  } }</pre>    <p><span style="font-family:'Courier New';">QuizMaster的值使用setQuizMaster()方法设置。在QuizMasterService类中,<span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;">QuizMaster对象没有实例化,但仍然来访问它。通常这样做会抛出<span style="font-family:'Courier New';">NullPointerException异常,但是容器已经实例化了这个对象,因此能很好的工作。</span></span></span></p>    <p><span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"><span style="font-family:'Courier New';">再作了这些改变之后,例子的类图就变成这样了。</span></span></p>    <p><span style="text-align:justify;widows:2;text-transform:none;background-color:#ffffff;text-indent:0px;display:inline !important;font:13px/19px Verdana, Arial, Helvetica, sans-serif;white-space:normal;orphans:2;float:none;letter-spacing:normal;color:#000000;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;"><span style="font-family:'Courier New';"><img title="Spring IoC 使用详解" border="0" alt="Spring IoC 使用详解" src="https://simg.open-open.com/show/9a7e4be455c5cb7733adda07ae84f73c.gif" width="618" height="382" /></span></span></p>    <p> 图中多了容器,它帮助注入依赖项。</p>    <p>在beans.xml文件中的beans配置:</p>    <p> </p>    <pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">   <bean id="springQuizMaster" class="com.vaannila.SpringQuizMaster"></bean>  <bean id="strutsQuizMaster" class="com.vaannila.StrutsQuizMaster"></bean>  <bean id="quizMasterService" class="com.vaannila.QuizMasterService">    <property name="quizMaster">    <ref local="springQuizMaster" />   </property>  </bean> </beans></pre>    <p>定义每个bean使用bean标签。bean标签的id属性是获取这个bean的逻辑名,class属性表示了真实的bean类。property标签指向bean的属性。使用setter注入一个bean需要使用ref标签。</p>    <p><span style="font-family:'Courier New';">SpringQuizMaster的引用被注入到QuizMasterbean。当我们执行这个例子,控制台打印出“Are you new to Spring?”。</span></p>    <p><span style="font-family:'Courier New';">为了使QuizMaster问一个struts相关的问题,仅仅需要改变ref标签的指向。</span></p>    <p> </p>    <pre class="brush:xml; toolbar: true; auto-links: false;"> <bean id="quizMasterService" class="com.vaannila.QuizMasterService">   <property name="quizMaster">     <ref local="strutsQuizMaster" />   </property>  </bean></pre>    <p>依赖注入降低了组件之间的耦合。</p>    <p>要执行这个例子需要把如下的jar文件加大classpath。</p>    <p>antlr-runtime-3.0<br /> commons-logging-1.0.4<br /> org.springframework.asm-3.0.0.M3<br /> org.springframework.beans-3.0.0.M3<br /> org.springframework.context-3.0.0.M3<br /> org.springframework.context.support-3.0.0.M3<br /> org.springframework.core-3.0.0.M3<br /> org.springframework.expression-3.0.0.M3</p>