Spring基於構造函數的依賴注入(DI)


以下內容引用自http://wiki.jikexueyuan.com/project/spring/dependency-injection/spring-constructor-based-dependency-injection.html

當容器調用帶有一組參數的類構造函數時,基於構造函數的DI就完成了,其中每個參數代表一個對其他類的依賴。

例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jsoft.testspring</groupId>
  <artifactId>testconstructor</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testconstructor</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency>
    
  </dependencies>
</project>

SpellChecker.java:

package com.jsoft.testspring.testconstructor;

public class SpellChecker {
    public SpellChecker(){
        System.out.println("SpellChecker無參數構造函數初始化");
    }
    
    public void checkSpelling(){
        System.out.println("SpellChecker檢查方法");
    }
}

TextEditor.java:

package com.jsoft.testspring.testconstructor;

public class TextEditor {
    private SpellChecker spellChecker;
    
    public TextEditor(SpellChecker spellChecker){
        System.out.println("TextEditor有參數構造函數初始化");
        this.spellChecker = spellChecker;
    }
    
    public void spellCheck() {
        this.spellChecker.checkSpelling();
    }
    
}

beans.xml:

<?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="spellChecker" class="com.jsoft.testspring.testconstructor.SpellChecker"></bean>
    
    <bean id="textEditor" class="com.jsoft.testspring.testconstructor.TextEditor">
        <constructor-arg ref="spellChecker"></constructor-arg>
    </bean>
   
</beans>

這里直接采用<constructor>節點指定TextEditor構造函數的參數。

App.java:

package com.jsoft.testspring.testconstructor;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        TextEditor textEditor = (TextEditor)applicationContext.getBean("textEditor");
        textEditor.spellCheck();
    }
}

運行結果:

構造函數參數解析:

如果存在不止一個參數時,當把參數傳遞給構造函數時,可能會存在歧義。要解決這個問題,那么構造函數的參數在bean定義中的順序,就是把這些參數提供給適當的構造函數參數的順序對應上就可以了。考慮下面的類:

package x.y;
public class Foo {
   public Foo(Bar bar, Baz baz) {
      // ...
   }
}

下述配置文件是能正常運行的:

<beans>
   <bean id="foo" class="x.y.Foo">
      <constructor-arg ref="bar"/> <constructor-arg ref="baz"/>
   </bean>

   <bean id="bar" class="x.y.Bar"/>
   <bean id="baz" class="x.y.Baz"/>
</beans>

只要把配置文件參數的順序對應上構造函數參數的順序即可。

讓我們再考慮一下我們傳遞給構造函數不同類型的位置。參考下面的類:

package x.y;
public class Foo {
   public Foo(int year, String name) {
      // ...
   }
}

如果你使用type屬性顯式的指定了構造函數參數的類型,容器也可以使用與簡單類型匹配的類型。例如:

<beans>

   <bean id="exampleBean" class="examples.ExampleBean">
      <constructor-arg type="int" value="2001"/> <constructor-arg type="java.lang.String" value="Zara"/>
   </bean>

</beans>

最后,最好的傳遞構造函數參數的方式,是使用index屬性來顯式的指定構造函數參數的索引。下面是基於索引為0的例子,如下所示:

<beans>

   <bean id="exampleBean" class="examples.ExampleBean">
      <constructor-arg index="0" value="2001"/> <constructor-arg index="1" value="Zara"/>
   </bean>

</beans>

 

測試工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test8/testconstructor


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM