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

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

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

例子:git

pom.xml:github

<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:spring

package com.jsoft.testspring.testconstructor;

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

TextEditor.java:apache

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:app

<?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構造函數的參數。maven

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

相關文章
相關標籤/搜索