Spring Dependency Injection 简明教程

Spring DI - Autowiring Constructor

该模式与 byType 非常相似,但适用于构造函数参数。Spring 容器查看在 XML 配置文件中将 autowire 属性设置为 constructor 的 bean。然后,它尝试匹配并将其构造函数的参数与配置中恰好一个 bean 名称关联。如果找到匹配项,它将注入这些 bean。否则,将不关联 bean。

This mode is very similar to byType, but it applies to constructor arguments. Spring container looks at the beans on which autowire attribute is set constructor in the XML configuration file. It then tries to match and wire its constructor’s argument with exactly one of the beans name in the configuration file. If matches are found, it will inject those beans. Otherwise, bean(s) will not be wired.

例如,如果将 bean 定义设置为在配置按构造函数自动关联,并且它有一个构造函数,该构造函数的一个参数为 SpellChecker 类型,Spring 将寻找名为 SpellChecker 的 bean 定义,并使用它设置构造函数的参数。您仍然可以使用 <constructor-arg> 标记关联剩余的参数。以下示例将说明这个概念。

For example, if a bean definition is set to autowire by constructor in configuration file, and it has a constructor with one of the arguments of SpellChecker type, Spring looks for a bean definition named SpellChecker, and uses it to set the constructor’s argument. Still you can wire remaining arguments using <constructor-arg> tags. The Following example will illustrate the concept.

Example

以下示例显示了一个只能使用构造函数进行依赖注入的 TextEditor 类。

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

让我们更新在 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;
   private String name;

   public TextEditor(SpellChecker spellChecker, String name) {
      this.spellChecker = spellChecker;
      this.name = name;
   }
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public String getName() {
      return name;
   }
   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 autowiring byName

<?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" autowire = "constructor">
      <constructor-arg name = "name" value = "Generic Text Editor" />
   </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 checkSpelling.