Spring Dependency Injection 简明教程
Spring DI - Overview
Spring 是一个最受欢迎的应用程序开发框架,用于企业级 Java。全世界数百万开发人员使用 Spring Framework 创建高性能、易于测试的代码,可以重复使用。
Spring is the most popular application development framework for enterprise Java. Millions of developers around the world use Spring Framework to create high performing, easily testable, and reusable code.
Spring Framework 是一个开源 Java 平台。它最初由 Rod Johnson 编写,并于 2003 年 6 月首次在 Apache 2.0 许可证下发布。
Spring framework is an open source Java platform. It was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.
Spring 提供 Ioc 容器,与 EJB 容器相比,它们往往更轻量级。这对在具有受限内存和 CPU 资源的计算机上开发和部署应用程序非常有利。
Spring provides Ioc Containers which tend to be lightweight, especially when compared to EJB containers, for example. This is beneficial for developing and deploying applications on computers with limited memory and CPU resources.
Dependency Injection (DI)
Spring 最常与其相关联的技术是反转控制的依赖项注入 (DI) 风格。反转控制 (IoC) 是一个通用概念,并且可以用许多不同的方式来表达。依赖项注入仅仅是反转控制的一个具体示例。
The technology that Spring is most identified with is the Dependency Injection (DI) flavor of Inversion of Control. The Inversion of Control (IoC) is a general concept, and it can be expressed in many different ways. Dependency Injection is merely one concrete example of Inversion of Control.
在编写复杂的 Java 应用程序时,应用程序类与其它的 Java 类应该尽可能独立,以提高在单元测试时独立于其他类来重复利用和测试这些类的可能性。依赖关系注入有助于将这些类粘合在一起,并同时保持它们的独立性。
When writing a complex Java application, application classes should be as independent as possible of other Java classes to increase the possibility to reuse these classes and to test them independently of other classes while unit testing. Dependency Injection helps in gluing these classes together and at the same time keeping them independent.
依赖关系注入到底是什么?让我们分别看一下这两个词。这里的依赖部分转换成了两个类之间的关联。例如,类 A 依赖于类 B。现在,让我们看一下第二部分,注入。它的全部含义是,类 B 将由 IoC 注入到类 A 中。
What is dependency injection exactly? Let’s look at these two words separately. Here the dependency part translates into an association between two classes. For example, class A is dependent of class B. Now, let’s look at the second part, injection. All this means is, class B will get injected into class A by the IoC.
依赖关系注入可以通过向构造函数传递参数或通过使用 setter 方法而在构建后执行。由于依赖关系注入是 Spring 框架的核心,因此我们将在一个单独的章节中使用相关的示例来解释这个概念。
Dependency injection can happen in the way of passing parameters to the constructor or by post-construction using setter methods. As Dependency Injection is the heart of Spring Framework, we will explain this concept in a separate chapter with relevant example.
Spring DI - Environment Setup
本章将在您开始使用 Spring 框架时,指导您如何准备开发环境。它还将教您在设置 Spring 框架之前如何在您的机器上设置 JDK、Maven 和 Eclipse −
This chapter will guide you on how to prepare a development environment to start your work with Spring Framework. It will also teach you how to set up JDK, Maven and Eclipse on your machine before you set up Spring Framework −
Setup Java Development Kit (JDK)
你可以从 Oracle 的 Java 站点下载 SDK 的最新版本 − Java SE Downloads. 你在下载文件中可以找到安装 JDK 的说明,按照给定的说明进行安装和配置。最后设置 PATH 和 JAVA_HOME 环境变量以引用包含 java 和 javac 的目录,通常分别为 java_install_dir/bin 和 java_install_dir。
You can download the latest version of SDK from Oracle’s Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.
如果您运行的是 Windows 且在 C:\jdk-11.0.11 中安装了 JDK,那么您将不得不把以下代码行放入您的 C:\autoexec.bat 文件中。
If you are running Windows and have installed the JDK in C:\jdk-11.0.11, you would have to put the following line in your C:\autoexec.bat file.
set PATH=C:\jdk-11.0.11;%PATH%
set JAVA_HOME=C:\jdk-11.0.11
或者,在 Windows NT/2000/XP 中,你必须右键单击我的电脑,选择属性 → 高级 → 环境变量。然后,你将不得不更新 PATH 值并单击确定按钮。
Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button.
在 Unix(Solaris、Linux 等)中,如果 SDK 安装在 /usr/local/jdk-11.0.11 且您使用 C shell,那么您将不得不把以下代码行放入您的 .cshrc 文件中。
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-11.0.11 and you use the C shell, you will have to put the following into your .cshrc file.
setenv PATH /usr/local/jdk-11.0.11/bin:$PATH
setenv JAVA_HOME /usr/local/jdk-11.0.11
或者,如果你使用诸如 Borland JBuilder、Eclipse、IntelliJ IDEA 或 Sun ONE Studio 这样的集成开发环境 (IDE),则必须编译并运行一个简单程序来确认 IDE 知道你在何处安装了 Java。否则,你必须按照 IDE 文档中给出的内容执行正确的设置。
Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE.
Setup Eclipse IDE
本教程中的所有示例都是使用 Eclipse IDE 编写的。因此,我们建议你应该在你机器上安装 Eclipse 的最新版本。
All the examples in this tutorial have been written using Eclipse IDE. So we would suggest you should have the latest version of Eclipse installed on your machine.
要安装 Eclipse IDE,请从 www.eclipse.org/downloads 下载最新的 Eclipse 二进制文件。下载安装文件后,将二进制分发包解压到方便的位置。例如,在 Windows 上的 C:\eclipse 中,或 Linux/Unix 上的 /usr/local/eclipse 中,最后相应地设置 PATH 变量。
To install Eclipse IDE, download the latest Eclipse binaries from www.eclipse.org/downloads. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:\eclipse on Windows, or /usr/local/eclipse on Linux/Unix and finally set PATH variable appropriately.
可以通过在 Windows 机器上执行以下命令启动 Eclipse,或者你只需双击 eclipse.exe
Eclipse can be started by executing the following commands on Windows machine, or you can simply double-click on eclipse.exe
%C:\eclipse\eclipse.exe
可以通过在 Unix(Solaris、Linux 等)机器上执行以下命令启动 Eclipse −
Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine −
$/usr/local/eclipse/eclipse
成功启动后,如果一切正常,它应该显示以下结果 −
After a successful startup, if everything is fine then it should display the following result −
Set Maven
在本教程中,我们使用 maven 来运行和构建基于 Spring 的示例。请按照 Maven - Environment Setup 安装 maven。
In this tutorial, we are using maven to run and build the spring based examples. Follow the Maven - Environment Setup to install maven.
Spring DI - IoC Containers
Spring 容器是 Spring Framework 的核心。容器将创建对象,将它们连接在一起,配置它们,并管理它们从创建到销毁的整个生命周期。Spring 容器使用 DI 来管理构成应用程序的组件。这些对象称为 Spring Bean,我们将在下一章中讨论它们。
The Spring container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction. The Spring container uses DI to manage the components that make up an application. These objects are called Spring Beans, which we will discuss in the next chapter.
容器通过读取提供的配置元数据来获取有关实例化、配置和组装对象的指令。该配置元数据可由 XML、Java 注释或 Java 代码表示。下图展示了 Spring 的工作原理的概述图。Spring IOC 容器利用 Java POJO 类和配置元数据来生成完全配置和可执行的系统或应用程序。
The container gets its instructions on what objects to instantiate, configure, and assemble by reading the configuration metadata provided. The configuration metadata can be represented either by XML, Java annotations, or Java code. The following diagram represents a high-level view of how Spring works. The Spring IoC container makes use of Java POJO classes and configuration metadata to produce a fully configured and executable system or application.
Spring 提供了下列两种不同类型的容器。
Spring provides the following two distinct types of containers.
Sr.No. |
Container & Description |
1 |
Spring BeanFactory Container This is the simplest container providing the basic support for DI and is defined by the org.springframework.beans.factory.BeanFactory interface. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purpose of backward compatibility with a large number of third-party frameworks that integrate with Spring. |
2 |
Spring ApplicationContext Container This container adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by the org.springframework.context.ApplicationContext interface. |
ApplicationContext 容器包含 BeanFactory 容器的所有功能,因此通常建议优于 BeanFactory。对于数据量和速度很重要的轻量级应用程序(如移动设备或基于小应用程序的应用程序),仍然可以使用 BeanFactory。
The ApplicationContext container includes all functionality of the BeanFactorycontainer, so it is generally recommended over BeanFactory. BeanFactory can still be used for lightweight applications like mobile devices or applet-based applications where data volume and speed is significant.
Spring DI - BeanFactory Container
这是提供基本 DI 支持的最简单的容器,由 org.springframework.beans.factory.BeanFactory 接口定义。BeanFactory 及其相关接口(例如 BeanFactoryAware、InitializingBean、DisposableBean)仍然存在于 Spring 中,以便向与 Spring 集成的众多第三方框架提供向后兼容性。
This is the simplest container providing the basic support for DI and defined by the org.springframework.beans.factory.BeanFactory interface. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purpose of backward compatibility with a large number of third-party frameworks that integrate with Spring.
有许多 BeanFactory 接口的实现是 Spring 中自带的。最常用的 BeanFactory 实现是 XmlBeanFactory 类。此容器从 XML 文件中读取配置元数据,并使用该元数据创建一个完全配置的系统或应用程序。
There are a number of implementations of the BeanFactory interface that are come straight out-of-the-box with Spring. The most commonly used BeanFactory implementation is the XmlBeanFactory class. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.
在诸如移动设备或基于小程序的应用程序等资源有限的情况下,通常首选 BeanFactory。因此,除非你有充分的理由不这样做,否则请使用 ApplicationContext。
The BeanFactory is usually preferred where the resources are limited like mobile devices or applet-based applications. Thus, use an ApplicationContext unless you have a good reason for not doing so.
Example
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
HelloWorld.java − A dependency class.
-
MainApp.java − Main application to run and test.
以下是 HelloWorld.java 文件的内容 −
Here is the content of HelloWorld.java file −
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
以下是第二个文件的内容 MainApp.java
Following is the content of the second file MainApp.java
package com.tutorialspoint;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class MainApp {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("Beans.xml"));
HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
obj.getMessage();
}
}
关于主程序,应注意以下两点:
Following two important points should be noted about the main program −
-
The first step is to create a factory object where we used the framework APIXmlBeanFactory() to create the factory bean andClassPathResource() API to load the bean configuration file available in CLASSPATH. The XmlBeanFactory() API takes care of creating and initializing all the objects, i.e. beans mentioned in the configuration file.
-
The second step is used to get the required bean using getBean() method of the created bean factory object. This method uses bean ID to return a generic object, which finally can be casted to the actual object. Once you have the object, you can use this object to call any class method.
以下是 bean 配置文件 Beans.xml 的内容
Following is the content of the bean configuration file 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-3.0.xsd">
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
Spring DI - ApplicationContext Container
ApplicationContext 是 Spring 的高级容器。它与 BeanFactory 类似,可以加载 bean 定义,将 bean 连接在一起,并在请求时分配 bean。此外,它还增加了更多特定于企业的功能,例如从属性文件中解析文本消息的能力以及向感兴趣的事件侦听器发布应用程序事件的能力。此容器由 org.springframework.context.ApplicationContext 接口定义。
The ApplicationContext is Spring’s advanced container. Similar to BeanFactory, it can load bean definitions, wire beans together, and dispense beans upon request. Additionally, it adds more enterprise-specific functionality such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners. This container is defined by org.springframework.context.ApplicationContext interface.
ApplicationContext 包含 BeanFactory 的所有功能,通常建议使用它来代替 BeanFactory。对于轻量级应用程序,例如移动设备或基于小程序的应用程序,仍然可以使用 BeanFactory。
The ApplicationContext includes all functionality of the BeanFactory, It is generally recommended over BeanFactory. BeanFactory can still be used for lightweight applications like mobile devices or applet-based applications.
最常用的 ApplicationContext 实现包括:
The most commonly used ApplicationContext implementations are −
-
FileSystemXmlApplicationContext − This container loads the definitions of the beans from an XML file. Here you need to provide the full path of the XML bean configuration file to the constructor.
-
ClassPathXmlApplicationContext − This container loads the definitions of the beans from an XML file. Here you do not need to provide the full path of the XML file but you need to set CLASSPATH properly because this container will look like bean configuration XML file in CLASSPATH.
-
WebXmlApplicationContext − This container loads the XML file with definitions of all beans from within a web application.
Example
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
HelloWorld.java − A dependency class.
-
MainApp.java − Main application to run and test.
以下是 HelloWorld.java 文件的内容 −
Here is the content of HelloWorld.java file −
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
以下是第二个文件 MainApp.java 的内容:
Following is the content of the second file MainApp.java −
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext
("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
关于主程序,应注意以下两点:
Following two important points should be noted about the main program −
-
The first step is to create factory object where we used framework API FileSystemXmlApplicationContext to create the factory bean after loading the bean configuration file from the given path. The FileSystemXmlApplicationContext() API takes care of creating and initializing all the objects ie. beans mentioned in the XML bean configuration file.
-
The second step is used to get the required bean using getBean() method of the created context. This method uses bean ID to return a generic object, which finally can be casted to the actual object. Once you have an object, you can use this object to call any class method.
以下是 bean 配置文件 Beans.xml 的内容
Following is the content of the bean configuration file 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-3.0.xsd">
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
Spring DI - Create Project
使用 Eclipse 选择 File → New * → *Maven Project 。勾选 创建简单项目(跳过原型选择),然后单击下一步。
Using eclipse, select File → New * → *Maven Project. Tick the *Create a simple project(skip archetype selection) * and click Next.
按照以下所示输入详细信息 −
Enter the details, as shown below −
-
groupId − com.tutorialspoint
-
artifactId − springdi
-
version − 0.0.1-SNAPSHOT
-
name − springdi
-
description − Spring Dependency Injection Project
单击完成按钮,将创建一个新项目。
Click on Finish button and an new project will be created.
pom.xml
使用 Spring Core 依赖项更新 pom.xml。以下是 pom.xml 的完整内容
Update the pom.xml with Spring Core dependency. Following is the full content of 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint</groupId>
<artifactId>springdi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springdi</name>
<description>Spring Dependency Injection Project</description>
<properties>
<org.springframework.version>5.3.9</org.springframework.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
applicationcontext.xml
在 *src → main → resources * 中创建 applicationcontext.xml,其内容如下。
Create applicationcontext.xml in *src → main → resources * with the following content.
applicationcontext.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-3.0.xsd">
</beans>
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 −
-
TextEditor.java − A class containing a SpellChecker as dependency.
-
SpellChecker.java − A dependency class.
-
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.
Spring DI - Injecting Inner Beans Constructor
众所周知,Java 内部类在其他类的范围内内定义,类似地, inner beans 是在另一个 bean 的范围内内定义的 bean。因此,<property/> 或 <constructor-arg/> 元素中的 <bean/> 元素被称为内部 bean,如下所示。
As you know Java inner classes are defined within the scope of other classes, similarly, inner beans are beans that are defined within the scope of another bean. Thus, a <bean/> element inside the <property/> or <constructor-arg/> elements is called inner bean and it is shown below.
<?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">
<bean id = "outerBean" class = "...">
<constructor-arg name = "target">
<bean id = "innerBean" class = "..."/>
</constructor-arg>
</bean>
</beans>
Example
以下示例显示了一个只能使用基于构造函数的注入进行依赖注入的类 TextEditor。
The following example shows a class TextEditor that can only be dependency-injected using constructor-based injection.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
TextEditor.java − A class containing a SpellChecker as dependency.
-
SpellChecker.java − A dependency class.
-
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;
}
// a getter method to return spellChecker
public SpellChecker getSpellChecker() {
return 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();
}
}
以下是已配置了基于 setter 的注入,但使用 inner beans 的配置文件 applicationcontext.xml −
Following is the configuration file applicationcontext.xml which has configuration for the setter-based injection but using inner beans −
<?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 using inner bean -->
<bean id = "textEditor" class = "com.tutorialspoint.TextEditor">
<constructor-arg name = "spellChecker">
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"/>
</constructor-arg>
</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.
Spring DI - Injecting Collections Constructor
您已经看到如何使用 Bean 配置文件中的 <property> 标记的 value 属性配置原始数据类型,以及使用 ref 属性配置对象引用。这两种情况都涉及将奇异值传递给 Bean。
You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
现在,如果您想传递像 Java Collection 类型(比如 List、Set 和 Properties)这样的复数值该怎么办?为了应对这种情况,Spring 提供了以下类型的集合配置元素,如下所示 −
Now what if you want to pass plural values like Java Collection types such as List, Set, and Properties. To handle the situation, Spring offers following types of collection configuration elements which are as follows −
Sr.No |
Element & Description |
1 |
<list> This helps in wiring ie injecting a list of values, allowing duplicates. |
2 |
<set> This helps in wiring a set of values but without any duplicates. |
3 |
<props> This can be used to inject a collection of name-value pairs where the name and value are both Strings. |
您可以使用 <list> 或 <set> 来接线任何 java.util.Collection 的实现或 array 。
You can use either <list> or <set> to wire any implementation of java.util.Collection or an array.
在该示例中,我们展示的是传递集合元素的直接值。
In this example, we’re showcasing passing direct values of the collection elements.
Example
下面的示例展示了 JavaCollection 类,它使用集合作为依赖项,这些集合使用构造函数参数进行注入。
The following example shows a class JavaCollection that is using collections as dependency injected using constructor arguments.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
JavaCollection.java − A class containing a collections as dependency.
-
MainApp.java − Main application to run and test.
以下是 JavaCollection.java 文件的内容−
Here is the content of JavaCollection.java file −
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
List<String> addressList;
Set<String> addressSet;
Properties addressProp;
public JavaCollection() {}
public JavaCollection(List<String> addressList, Set<String> addressSet,
Properties addressProp) {
this.addressList = addressList;
this.addressSet = addressSet;
this.addressProp = addressProp;
}
// a setter method to set List
public void setAddressList(List<String> addressList) {
this.addressList = addressList;
}
// prints and returns all the elements of the list.
public List<String> getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
// a setter method to set Set
public void setAddressSet(Set<String> addressSet) {
this.addressSet = addressSet;
}
// prints and returns all the elements of the Set.
public Set<String> getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
// a setter method to set Property
public void setAddressProp(Properties addressProp) {
this.addressProp = addressProp;
}
// prints and returns all the elements of the Property.
public Properties getAddressProp() {
System.out.println("Property Elements :" + addressProp);
return addressProp;
}
}
以下是 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");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressList();
jc.getAddressSet();
jc.getAddressProp();
}
}
以下是配置文件 applicationcontext.xml ,其中包含了所有类型的集合配置 −
Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −
<?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">
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<constructor-arg name = "addressList">
<list>
<value>INDIA</value>
<value>JAPAN</value>
<value>USA</value>
<value>UK</value>
</list>
</constructor-arg>
<constructor-arg name = "addressSet">
<set>
<value>INDIA</value>
<value>JAPAN</value>
<value>USA</value>
<value>UK</value>
</set>
</constructor-arg>
<constructor-arg name = "addressProp">
<props>
<prop key = "one">INDIA</prop>
<prop key = "two">JAPAN</prop>
<prop key = "three">USA</prop>
<prop key = "four">UK</prop>
</props>
</constructor-arg>
</bean>
</beans>
完成源文件和 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 −
List Elements :[INDIA, JAPAN, USA, UK]
Set Elements :[INDIA, JAPAN, USA, UK]
Property Elements :{four=UK, one=INDIA, two=JAPAN, three=USA}
Spring DI - Injecting Collections Ref Constructor
您已经看到如何使用 Bean 配置文件中的 <property> 标记的 value 属性配置原始数据类型,以及使用 ref 属性配置对象引用。这两种情况都涉及将奇异值传递给 Bean。
You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
现在,如果您想传递像 Java Collection 类型(比如 List、Set 和 Properties)这样的复数值该怎么办?为了应对这种情况,Spring 提供了以下类型的集合配置元素,如下所示 −
Now what if you want to pass plural values like Java Collection types such as List, Set, and Properties. To handle the situation, Spring offers following types of collection configuration elements which are as follows −
Sr.No |
Element & Description |
1 |
<list> This helps in wiring ie injecting a list of values, allowing duplicates. |
2 |
<set> This helps in wiring a set of values but without any duplicates. |
您可以使用 <list> 或 <set> 来接线任何 java.util.Collection 的实现或 array 。
You can use either <list> or <set> to wire any implementation of java.util.Collection or an array.
在此示例中,我们展示了使用 ref 传递集合元素。
In this example, we’re showcasing passing collection elements using ref.
Example
以下示例展示了一个 JavaCollection 类,它使用通过 setter 注入的依赖关系集合。
The following example shows a class JavaCollection that is using collection of dependencies injected using setters.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
Address.java − A class to be used as dependency.
-
JavaCollection.java − A class containing a collections of dependencies.
-
MainApp.java − Main application to run and test.
以下是 Address.java 文件的内容 −
Here is the content of Address.java file −
package com.tutorialspoint;
public class Address {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
以下是 JavaCollection.java 文件的内容−
Here is the content of JavaCollection.java file −
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
List<Address> addressList;
Set<Address> addressSet;
public JavaCollection(List<Address> addressList, Set<Address> addressSet) {
this.addressList = addressList;
this.addressSet = addressSet;
}
// a setter method to set List
public void setAddressList(List<Address> addressList) {
this.addressList = addressList;
}
// prints and returns all the elements of the list.
public List<Address> getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
// a setter method to set Set
public void setAddressSet(Set<Address> addressSet) {
this.addressSet = addressSet;
}
// prints and returns all the elements of the Set.
public Set<Address> getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
}
以下是 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");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressList();
jc.getAddressSet();
}
}
以下是配置文件 applicationcontext.xml ,其中包含了所有类型的集合配置 −
Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −
<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">
<bean id = "address1" class = "com.tutorialspoint.Address">
<property name="name" value="INDIA"></property>
</bean>
<bean id = "address2" class = "com.tutorialspoint.Address">
<property name="name" value="JAPAN"></property>
</bean>
<bean id = "address3" class = "com.tutorialspoint.Address">
<property name="name" value="USA"></property>
</bean>
<bean id = "address4" class = "com.tutorialspoint.Address">
<property name="name" value="UK"></property>
</bean>
<!-- Definition for javaCollection -->
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<constructor-arg name = "addressList">
<list>
<ref bean="address1" />
<ref bean="address2" />
<ref bean="address3" />
<ref bean="address4" />
</list>
</constructor-arg>
<constructor-arg name = "addressSet">
<set>
<ref bean="address1" />
<ref bean="address2" />
<ref bean="address3" />
<ref bean="address4" />
</set>
</constructor-arg>
</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 −
List Elements :[INDIA, JAPAN, USA, UK]
Set Elements :[INDIA, JAPAN, USA, UK]
Spring DI - Injecting Map Constructor
您已经看到如何使用 Bean 配置文件中的 <property> 标记的 value 属性配置原始数据类型,以及使用 ref 属性配置对象引用。这两种情况都涉及将奇异值传递给 Bean。
You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
现在,如果你想传递 Map,在这个示例中,我们将展示如何使用构造函数注入传递 Map 的直接值。
Now what if you want to pass Map. In this example, we’re showcasing passing direct values of the Map using constructor injection.
Example
下面的示例展示了 JavaCollection 类,它使用集合作为依赖项,这些集合使用构造函数参数进行注入。
The following example shows a class JavaCollection that is using collections as dependency injected using constructor arguments.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
JavaCollection.java − A class containing a collections as dependency.
-
MainApp.java − Main application to run and test.
以下是 JavaCollection.java 文件的内容−
Here is the content of JavaCollection.java file −
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
Map<String, String> addressMap;
public JavaCollection() {}
public JavaCollection(Map<String, String> addressMap) {
this.addressMap = addressMap;
}
// a setter method to set Map
public void setAddressMap(Map<String, String> addressMap) {
this.addressMap = addressMap;
}
// prints and returns all the elements of the Map.
public Map<String, String> getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
}
以下是 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");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressMap();
}
}
以下是配置文件 applicationcontext.xml ,其中包含了所有类型的集合配置 −
Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −
<?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">
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<constructor-arg name = "addressMap">
<map>
<entry key = "1" value = "INDIA"/>
<entry key = "2" value = "JAPAN"/>
<entry key = "3" value = "USA"/>
<entry key = "4" value = "UK"/>
</map>
</constructor-arg>
</bean>
</beans>
Spring DI - Map Ref Constructor
您已经看到如何使用 Bean 配置文件中的 <property> 标记的 value 属性配置原始数据类型,以及使用 ref 属性配置对象引用。这两种情况都涉及将奇异值传递给 Bean。
You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
现在,如果你想传递 Map,在这个示例中,我们将展示如何使用构造函数注入传递 Map 的直接值。
Now what if you want to pass Map. In this example, we’re showcasing passing direct values of the Map using constructor injection.
Example
下面的示例展示了 JavaCollection 类,它使用集合作为依赖项,这些集合使用构造函数参数进行注入。
The following example shows a class JavaCollection that is using collections as dependency injected using constructor arguments.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
Address.java − A class to be used as dependency.
-
JavaCollection.java − A class containing a collections of dependencies.
-
MainApp.java − Main application to run and test.
以下是 Address.java 文件的内容 −
Here is the content of Address.java file −
package com.tutorialspoint;
public class Address {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
以下是 JavaCollection.java 文件的内容−
Here is the content of JavaCollection.java file −
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
Map<String, Address> addressMap;
public JavaCollection() {}
public JavaCollection(Map<String, Address> addressMap) {
this.addressMap = addressMap;
}
// a setter method to set Map
public void setAddressMap(Map<String, Address> addressMap) {
this.addressMap = addressMap;
}
// prints and returns all the elements of the Map.
public Map<String, Address> getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
}
以下是 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");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressMap();
}
}
以下是配置文件 applicationcontext.xml ,其中包含了所有类型的集合配置 −
Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −
<?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">
<bean id = "address1" class = "com.tutorialspoint.Address">
<property name="name" value="INDIA"></property>
</bean>
<bean id = "address2" class = "com.tutorialspoint.Address">
<property name="name" value="JAPAN"></property>
</bean>
<bean id = "address3" class = "com.tutorialspoint.Address">
<property name="name" value="USA"></property>
</bean>
<bean id = "address4" class = "com.tutorialspoint.Address">
<property name="name" value="UK"></property>
</bean>
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<constructor-arg name = "addressMap">
<map>
<entry key = "1" value-ref = "address1"/>
<entry key = "2" value-ref = "address2"/>
<entry key = "3" value-ref = "address3"/>
<entry key = "4" value-ref = "address4"/>
</map>
</constructor-arg>
</bean>
</beans>
完成源文件和 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 −
Map Elements :{1=INDIA, 2=JAPAN, 3=USA, 4=UK}
Spring DI - Setter-Based
基于 Setter 的 DI 由在调用无参数构造函数或无参数静态工厂方法来实例化 bean 之后,容器通过调用你 bean 上的 setter 方法来完成。
Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
Example
以下示例展示了 TextEditor 类,它只能使用纯基于 setter 的注入进行依赖项注入。
The following example shows a class TextEditor that can only be dependency-injected using pure setter-based injection.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
TextEditor.java − A class containing a SpellChecker as dependency.
-
SpellChecker.java − A dependency class.
-
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;
// a setter method to inject the dependency.
public void setSpellChecker(SpellChecker spellChecker) {
System.out.println("Inside setSpellChecker." );
this.spellChecker = spellChecker;
}
// a getter method to return spellChecker
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
此处你需要检查 setter 方法的命名约定。要设置变量 spellChecker ,我们要使用 setSpellChecker() 方法,它与 Java POJO 类非常相似。让我们创建另一个依赖类文件的 SpellChecker.java 内容
Here you need to check the naming convention of the setter methods. To set a variable spellChecker we are using setSpellChecker() method which is very similar to Java POJO classes. Let us create 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();
}
}
以下是用于基于 setter 的注入的配置的配置文件 applicationcontext.xml
Following is the configuration file applicationcontext.xml which has configuration for the setter-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">
<property name = "spellChecker" ref = "spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean>
</beans>
您应当注意构造函数注入和基于 Setter 注入中 applicationcontext.xml 文件定义中的差异。唯一差异在于 <bean> 元素内部,我们对基于构造函数的注入使用了 <constructor-arg> 标签,对基于 Setter 的注入使用了 <property> 标签。
You should note the difference in applicationcontext.xml file defined in the constructor-based injection and the setter-based injection. The only difference is inside the <bean> element where we have used <constructor-arg> tags for constructor-based injection and <property> tags for setter-based injection.
需要记住的第二项重要内容是如果您传递了对象引用,则需要使用 <property> 标签的 ref 属性,如果您直接传递了 value ,则应该使用 value 属性。
The second important point to note is that in case you are passing a reference to an object, you need to use ref attribute of <property> tag and if you are passing a value directly then you should use value attribute.
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, this will print the following message −
Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.
Spring DI - Inner Beans Setter
众所周知,Java 内部类在其他类的范围内内定义,类似地, inner beans 是在另一个 bean 的范围内内定义的 bean。因此,<property/> 或 <constructor-arg/> 元素中的 <bean/> 元素被称为内部 bean,如下所示。
As you know Java inner classes are defined within the scope of other classes, similarly, inner beans are beans that are defined within the scope of another bean. Thus, a <bean/> element inside the <property/> or <constructor-arg/> elements is called inner bean and it is shown below.
<?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">
<bean id = "outerBean" class = "...">
<property name = "target">
<bean id = "innerBean" class = "..."/>
</property>
</bean>
</beans>
Example
以下示例展示了 TextEditor 类,它只能使用纯基于 setter 的注入进行依赖项注入。
The following example shows a class TextEditor that can only be dependency-injected using pure setter-based injection.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
TextEditor.java − A class containing a SpellChecker as dependency.
-
SpellChecker.java − A dependency class.
-
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;
// a setter method to inject the dependency.
public void setSpellChecker(SpellChecker spellChecker) {
System.out.println("Inside setSpellChecker." );
this.spellChecker = spellChecker;
}
// a getter method to return spellChecker
public SpellChecker getSpellChecker() {
return 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();
}
}
以下是已配置了基于 setter 的注入,但使用 inner beans 的配置文件 applicationcontext.xml −
Following is the configuration file applicationcontext.xml which has configuration for the setter-based injection but using inner beans −
<?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 using inner bean -->
<bean id = "textEditor" class = "com.tutorialspoint.TextEditor">
<property name = "spellChecker">
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"/>
</property>
</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 setSpellChecker.
Inside checkSpelling.
Spring DI - Collections Setter
您已经看到如何使用 Bean 配置文件中的 <property> 标记的 value 属性配置原始数据类型,以及使用 ref 属性配置对象引用。这两种情况都涉及将奇异值传递给 Bean。
You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
现在,如果您想传递 Java 集合类型之类的复数值,例如列表、集合、映射和属性,该怎么办。为了应对该情况,Spring 提供了以下类型的集合配置元素:
Now what if you want to pass plural values like Java Collection types such as List, Set, Map, and Properties. To handle the situation, Spring offers following types of collection configuration elements which are as follows −
Sr.No |
Element & Description |
1 |
<list> This helps in wiring ie injecting a list of values, allowing duplicates. |
2 |
<set> This helps in wiring a set of values but without any duplicates. |
3 |
<props> This can be used to inject a collection of name-value pairs where the name and value are both Strings. |
您可以使用 <list> 或 <set> 来接线任何 java.util.Collection 的实现或 array 。
You can use either <list> or <set> to wire any implementation of java.util.Collection or an array.
在该示例中,我们展示的是传递集合元素的直接值。
In this example, we’re showcasing passing direct values of the collection elements.
Example
以下示例显示了一个使用集合作为 Setter 注入的依赖项的 JavaCollection 类。
The following example shows a class JavaCollection that is using collections as dependency injected using setters.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
JavaCollection.java − A class containing a collections as dependency.
-
MainApp.java − Main application to run and test.
以下是 JavaCollection.java 文件的内容−
Here is the content of JavaCollection.java file −
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
List<String> addressList;
Set<String> addressSet;
Properties addressProp;
// a setter method to set List
public void setAddressList(List<String> addressList) {
this.addressList = addressList;
}
// prints and returns all the elements of the list.
public List<String> getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
// a setter method to set Set
public void setAddressSet(Set<String> addressSet) {
this.addressSet = addressSet;
}
// prints and returns all the elements of the Set.
public Set<String> getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
// a setter method to set Property
public void setAddressProp(Properties addressProp) {
this.addressProp = addressProp;
}
// prints and returns all the elements of the Property.
public Properties getAddressProp() {
System.out.println("Property Elements :" + addressProp);
return addressProp;
}
}
以下是 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");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressList();
jc.getAddressSet();
jc.getAddressProp();
}
}
以下是配置文件 applicationcontext.xml ,其中包含了所有类型的集合配置 −
Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −
<?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 javaCollection -->
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<!-- results in a setAddressList(java.util.List) call -->
<property name = "addressList">
<list>
<value>INDIA</value>
<value>JAPAN</value>
<value>USA</value>
<value>UK</value>
</list>
</property>
<!-- results in a setAddressSet(java.util.Set) call -->
<property name = "addressSet">
<set>
<value>INDIA</value>
<value>JAPAN</value>
<value>USA</value>
<value>UK</value>
</set>
</property>
<!-- results in a setAddressProp(java.util.Properties) call -->
<property name = "addressProp">
<props>
<prop key = "one">INDIA</prop>
<prop key = "two">JAPAN</prop>
<prop key = "three">USA</prop>
<prop key = "four">UK</prop>
</props>
</property>
</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 −
List Elements :[INDIA, JAPAN, USA, UK]
Set Elements :[INDIA, JAPAN, USA, UK]
Property Elements :{four=UK, one=INDIA, two=JAPAN, three=USA}
Sprint DI - Collections Ref Setter
您已经看到如何使用 Bean 配置文件中的 <property> 标记的 value 属性配置原始数据类型,以及使用 ref 属性配置对象引用。这两种情况都涉及将奇异值传递给 Bean。
You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
现在,如果您想传递 Java 集合类型之类的复数值,例如列表、集合、映射和属性,该怎么办。为了应对该情况,Spring 提供了以下类型的集合配置元素:
Now what if you want to pass plural values like Java Collection types such as List, Set, Map, and Properties. To handle the situation, Spring offers following types of collection configuration elements which are as follows −
Sr.No |
Element & Description |
1 |
<list> This helps in wiring ie injecting a list of values, allowing duplicates. |
2 |
<set> This helps in wiring a set of values but without any duplicates. |
您可以使用 <list> 或 <set> 来接线任何 java.util.Collection 的实现或 array 。
You can use either <list> or <set> to wire any implementation of java.util.Collection or an array.
在此示例中,我们展示了使用 ref 传递集合元素。
In this example, we’re showcasing passing collection elements using ref.
Example
以下示例展示了一个 JavaCollection 类,它使用通过 setter 注入的依赖关系集合。
The following example shows a class JavaCollection that is using collection of dependencies injected using setters.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
Address.java − A class to be used as dependency.
-
JavaCollection.java − A class containing a collections of dependencies.
-
MainApp.java − Main application to run and test.
以下是 Address.java 文件的内容 −
Here is the content of Address.java file −
package com.tutorialspoint;
public class Address {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
以下是 JavaCollection.java 文件的内容−
Here is the content of JavaCollection.java file −
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
List<Address> addressList;
Set<Address> addressSet;
// a setter method to set List
public void setAddressList(List<Address> addressList) {
this.addressList = addressList;
}
// prints and returns all the elements of the list.
public List<Address> getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
// a setter method to set Set
public void setAddressSet(Set<Address> addressSet) {
this.addressSet = addressSet;
}
// prints and returns all the elements of the Set.
public Set<Address> getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
}
以下是 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");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressList();
jc.getAddressSet();
}
}
以下是配置文件 applicationcontext.xml ,其中包含了所有类型的集合配置 −
Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −
<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">
<bean id = "address1" class = "com.tutorialspoint.Address">
<property name="name" value="INDIA"></property>
</bean>
<bean id = "address2" class = "com.tutorialspoint.Address">
<property name="name" value="JAPAN"></property>
</bean>
<bean id = "address3" class = "com.tutorialspoint.Address">
<property name="name" value="USA"></property>
</bean>
<bean id = "address4" class = "com.tutorialspoint.Address">
<property name="name" value="UK"></property>
</bean>
<!-- Definition for javaCollection -->
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<!-- results in a setAddressList(java.util.List) call -->
<property name = "addressList">
<list>
<ref bean="address1" />
<ref bean="address2" />
<ref bean="address3" />
<ref bean="address4" />
</list>
</property>
<!-- results in a setAddressSet(java.util.Set) call -->
<property name = "addressSet">
<set>
<ref bean="address1" />
<ref bean="address2" />
<ref bean="address3" />
<ref bean="address4" />
</set>
</property>
</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 −
List Elements :[INDIA, JAPAN, USA, UK]
Set Elements :[INDIA, JAPAN, USA, UK]
Spring DI - Map Setter
您已经看到如何使用 Bean 配置文件中的 <property> 标记的 value 属性配置原始数据类型,以及使用 ref 属性配置对象引用。这两种情况都涉及将奇异值传递给 Bean。
You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
现在,如果你想传递 Map,该怎么办?在本示例中,我们展示使用 setter 注入传递 Map 的直接值。
Now what if you want to pass Map. In this example, we’re showcasing passing direct values of the Map using setter injection.
Example
以下示例展示了一个类 JavaCollection,该类使用集合作为依赖关系,并使用 setter 方法进行注入。
The following example shows a class JavaCollection that is using collections as dependency injected using setter method.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
JavaCollection.java − A class containing a collections as dependency.
-
MainApp.java − Main application to run and test.
以下是 JavaCollection.java 文件的内容−
Here is the content of JavaCollection.java file −
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
Map<String, String> addressMap;
public JavaCollection() {}
public JavaCollection(Map<String, String> addressMap) {
this.addressMap = addressMap;
}
// a setter method to set Map
public void setAddressMap(Map<String, String> addressMap) {
this.addressMap = addressMap;
}
// prints and returns all the elements of the Map.
public Map<String, String> getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
}
以下是 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");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressMap();
}
}
以下是配置文件 applicationcontext.xml ,其中包含了所有类型的集合配置 −
Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −
<?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">
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<property name = "addressMap">
<map>
<entry key = "1" value = "INDIA"/>
<entry key = "2" value = "JAPAN"/>
<entry key = "3" value = "USA"/>
<entry key = "4" value = "UK"/>
</map>
</property>
</bean>
</beans>
Spring DI - Map Ref Setter
您已经看到如何使用 Bean 配置文件中的 <property> 标记的 value 属性配置原始数据类型,以及使用 ref 属性配置对象引用。这两种情况都涉及将奇异值传递给 Bean。
You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
现在,如果你想传递 Map,该怎么办?在本示例中,我们展示使用 setter 注入传递 Map 的直接值。
Now what if you want to pass Map. In this example, we’re showcasing passing direct values of the Map using setter injection.
Example
以下示例展示了一个类 JavaCollection,该类使用集合作为依赖关系,并使用 setter 方法进行注入。
The following example shows a class JavaCollection that is using collections as dependency injected using setter method.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
Address.java − A class to be used as dependency.
-
JavaCollection.java − A class containing a collections of dependencies.
-
MainApp.java − Main application to run and test.
以下是 Address.java 文件的内容 −
Here is the content of Address.java file −
package com.tutorialspoint;
public class Address {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
以下是 JavaCollection.java 文件的内容−
Here is the content of JavaCollection.java file −
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
Map<String, Address> addressMap;
public JavaCollection() {}
public JavaCollection(Map<String, Address> addressMap) {
this.addressMap = addressMap;
}
// a setter method to set Map
public void setAddressMap(Map<String, Address> addressMap) {
this.addressMap = addressMap;
}
// prints and returns all the elements of the Map.
public Map<String, Address> getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
}
以下是 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");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressMap();
}
}
以下是配置文件 applicationcontext.xml ,其中包含了所有类型的集合配置 −
Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −
<?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">
<bean id = "address1" class = "com.tutorialspoint.Address">
<property name="name" value="INDIA"></property>
</bean>
<bean id = "address2" class = "com.tutorialspoint.Address">
<property name="name" value="JAPAN"></property>
</bean>
<bean id = "address3" class = "com.tutorialspoint.Address">
<property name="name" value="USA"></property>
</bean>
<bean id = "address4" class = "com.tutorialspoint.Address">
<property name="name" value="UK"></property>
</bean>
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<property name = "addressMap">
<map>
<entry key = "1" value-ref = "address1"/>
<entry key = "2" value-ref = "address2"/>
<entry key = "3" value-ref = "address3"/>
<entry key = "4" value-ref = "address4"/>
</map>
</property>
</bean>
</beans>
Spring DI - Autowiring
你已了解如何使用 <bean> 元素声明 bean,并使用 XML 配置文件中的 <constructor-arg> 和 <property> 元素注入 <bean>。
You have learnt how to declare beans using the <bean> element and inject <bean> using <constructor-arg> and <property> elements in XML configuration file.
Spring 容器可以在不使用 <constructor-arg> 和 <property> 元素的情况下确定协作 bean 之间的关系,这有助于减少为大型基于 Spring 的应用程序编写的 XML 配置量。
The Spring container can autowire relationships between collaborating beans without using <constructor-arg> and <property> elements, which helps cut down on the amount of XML configuration you write for a big Spring-based application.
Autowiring Modes
以下是自动装配模式,可以用于指示 Spring 容器对依赖项注入使用自动装配。你可以使用 <bean/> 元素的 autowire 属性来为 bean 定义指定 autowire 模式。
Following are the autowiring modes, which can be used to instruct the Spring container to use autowiring for dependency injection. You use the autowire attribute of the <bean/> element to specify autowire mode for a bean definition.
Sr.No |
Mode & Description |
1 |
no This is default setting which means no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring. This is what you already have seen in Dependency Injection chapter. |
2 |
byName Autowiring by property name. Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file. |
3 |
byType Autowiring by property datatype. Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown. |
4 |
constructor Similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised. |
5 |
autodetect Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType. |
你可以使用 byType 或 constructor 自动装配模式来连接数组和其他类型集合。
You can use byType or constructor autowiring mode to wire arrays and other typed-collections.
Limitations with autowiring
当在整个项目中一致使用时,自动装配效果最好。如果通常不使用自动装配,开发人员仅将其用于连接一个或两个 Bean 定义可能会令人迷惑。尽管如此,自动装配可以显著减少指定属性或构造函数参数的需要,但你应在使用自动装配前考虑其局限性和缺点。
Autowiring works best when it is used consistently across a project. If autowiring is not used in general, it might be confusing for developers to use it to wire only one or two bean definitions. Though, autowiring can significantly reduce the need to specify properties or constructor arguments but you should consider the limitations and disadvantages of autowiring before using them.
Sr.No. |
Limitations & Description |
1 |
Overriding possibility You can still specify dependencies using <constructor-arg> and <property> settings which will always override autowiring. |
2 |
Primitive data types You cannot autowire so-called simple properties such as primitives, Strings, and Classes. |
3 |
Confusing nature Autowiring is less exact than explicit wiring, so if possible prefer using explict wiring. |
Spring DI - Autowiring ByName
该模式指定按属性名称自动注入。Spring 容器查看在 XML 配置文件中将 auto-wire 属性设置为 byName 的 bean。然后,它尝试匹配并将其属性与配置中由相同名称定义的 bean 进行关联。如果找到匹配项,它将注入这些 bean。否则,将不关联 bean。
This mode specifies autowiring by property name. Spring container looks at the beans on which auto-wire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file. If matches are found, it will inject those beans. Otherwise, bean(s) will not be wired.
例如,如果将 bean 定义设置为在配置按 byName 自动关联,并且它包含一个 spellChecker 属性(也就是说,它有一个 setSpellChecker(…)方法),Spring 将寻找名为 spellChecker 的 bean 定义,并使用它设置该属性。您仍然可以使用 <property> 标记关联剩余的属性。以下示例将说明该概念。
For example, if a bean definition is set to autowire byName in the configuration file, and it contains a spellChecker property (that is, it has a setSpellChecker(…)method), Spring looks for a bean definition named spellChecker, and uses it to set the property. Still you can wire the remaining properties using <property> tags. The following example will illustrate the concept.
Example
以下示例展示了 TextEditor 类,它只能使用纯基于 setter 的注入进行依赖项注入。
The following example shows a class TextEditor that can only be dependency-injected using pure setter-based injection.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
TextEditor.java − A class containing a SpellChecker as dependency.
-
SpellChecker.java − A dependency class.
-
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 void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void setName(String name) {
this.name = name;
}
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 = "byName">
<property name = "name" value = "Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean>
</beans>
Spring DI - Autowiring ByType
这种模式通过属性类型指定自动装配。Spring 容器查找那些在 XML 配置文件中将自动装配属性设置为按类型(byType)的 Bean。然后,它尝试匹配和连接一个属性,如果它的 type 准确地匹配配置文件中的一个 Bean 名称。如果找到匹配,它将注入这些 Bean。否则,将不会连接 Bean。
This mode specifies autowiring by property type. Spring container looks at the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches 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 类型名为 spellChecker 的属性,Spring 将查找名为 SpellChecker 的 Bean 定义,并使用它来设置该属性。您仍然可以使用 <property> 标记连接剩余的属性。以下示例将说明这个概念。
For example, if a bean definition is set to autowire byType in the configuration file, and it contains a spellChecker property of SpellChecker type, Spring looks for a bean definition named SpellChecker, and uses it to set the property. Still you can wire the remaining properties using <property> tags. The following example will illustrate the concept.
Example
以下示例展示了 TextEditor 类,它只能使用纯基于 setter 的注入进行依赖项注入。
The following example shows a class TextEditor that can only be dependency-injected using pure setter-based injection.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
TextEditor.java − A class containing a SpellChecker as dependency.
-
SpellChecker.java − A dependency class.
-
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 void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void setName(String name) {
this.name = name;
}
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 = "byType">
<property name = "name" value = "Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean>
</beans>
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 −
-
TextEditor.java − A class containing a SpellChecker as dependency.
-
SpellChecker.java − A dependency class.
-
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>
Spring DI - Static Factory
Spring 提供了一个使用 factory-method 属性注入相关依赖项的选项。
Spring provides an option to inject dependency using factory-method attribute.
Example
以下示例展示了 TextEditor 类,它只能使用纯基于 setter 的注入进行依赖项注入。
The following example shows a class TextEditor that can only be dependency-injected using pure setter-based injection.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
TextEditor.java − A class containing a SpellChecker as dependency.
-
SpellChecker.java − A dependency class.
-
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 void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
以下是另一个依赖类文件 SpellChecker.java 的内容 −
Following is the content of another dependent class file SpellChecker.java −
这个类构造函数是私有的。所以无法用 new 运算符直接用其他对象创建其对象。它有一个静态工厂方法来获取一个实例。
This class constructor is private. So its object can not be created directly using new operator by other object. It has a static factory method to get an instance.
package com.tutorialspoint;
public class SpellChecker {
private SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public static SpellChecker getInstance() {
System.out.println("Inside SpellChecker getInstance." );
return new SpellChecker();
}
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 = "byName">
<property name = "name" value = "Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker" factory-method="getInstance"></bean>
</beans>
完成源文件和 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 getInstance.
Inside SpellChecker constructor.
Inside checkSpelling.
Spring DI - Non-Static Factory
Spring 提供了一个选项,可以使用工厂方法和工厂 bean 属性注入依赖项,以用于非静态工厂方法。
Spring provides an option to inject dependency using factory-method along with factory-bean attributes in case of non-static factory methods.
Example
以下示例展示了 TextEditor 类,它只能使用纯基于 setter 的注入进行依赖项注入。
The following example shows a class TextEditor that can only be dependency-injected using pure setter-based injection.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
TextEditor.java − A class containing a SpellChecker as dependency.
-
SpellChecker.java − A dependency class.
-
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 void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
以下是另一个依赖类文件 SpellChecker.java 的内容 −
Following is the content of another dependent class file SpellChecker.java −
这个类构造函数是私有的。因此,无法使用 new 操作符直接使用它创建其他对象。它使用非静态工厂方法来获取一个实例。
This class constructor is private. So its object can not be created directly using new operator by other object. It has a non-static factory method to get an instance.
package com.tutorialspoint;
public class SpellChecker {
private SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public SpellChecker getInstance() {
System.out.println("Inside SpellChecker getInstance." );
return new SpellChecker();
}
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 = "byName">
<property name = "name" value = "Generic Text Editor" />
</bean>
<bean id = "spellCheckFactory" class = "com.tutorialspoint.SpellChecker"></bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker" factory-method="getInstance">< factory-bean="spellCheckFactory"/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 SpellChecker getInstance.
Inside SpellChecker constructor.
Inside checkSpelling.