Spring Dependency Injection 简明教程

Spring DI - Constructor-Based

当容器使用一系列参数调用类构造函数,每个参数表示对其他类的依赖关系时,就完成了基于构造函数的 DI。

Constructor-Based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on the other class.

Example

以下示例展示了一个类 TextEditor,它只能通过构造函数注入进行依赖注入。

The following example shows a class TextEditor that can only be dependency-injected with constructor injection.

让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −

Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −

  1. TextEditor.java − A class containing a SpellChecker as dependency.

  2. SpellChecker.java − A dependency class.

  3. MainApp.java − Main application to run and test.

以下为 TextEditor.java 文件的内容 −

Here is the content of TextEditor.java file −

package com.tutorialspoint;

public class TextEditor {
   private SpellChecker spellChecker;

   public TextEditor(SpellChecker spellChecker) {
      System.out.println("Inside TextEditor constructor." );
      this.spellChecker = spellChecker;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

以下是另一个依赖类文件 SpellChecker.java 的内容

Following is the content of another dependent class file SpellChecker.java

package com.tutorialspoint;
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   }
}

以下是 MainApp.java 文件的内容。

Following is the content of the MainApp.java file.

package com.tutorialspoint;

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

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");

      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   }
}

以下是 applicationcontext.xml 配置文件,它包含基于构造函数的注入配置 −

Following is the configuration file applicationcontext.xml which has configuration for the constructor-based injection −

<?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-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id = "textEditor" class = "com.tutorialspoint.TextEditor">
      <constructor-arg ref = "spellChecker"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean>
</beans>

Output

完成源文件和 Bean 配置文件创建后,我们运行该应用程序。如果您的应用程序一切正常,它将打印以下消息−

Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message −

Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.