XML-Based Injection in Spring – Spring中基于XML的注入

最后修改: 2017年 11月 8日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this basic tutorial, we’ll learn how to do simple XML-based bean configuration with the Spring Framework.

在这个基础教程中,我们将学习如何用Spring框架做简单的基于XML的bean配置。

2. Overview

2.概述

Let’s start by adding Spring’s library dependency in the pom.xml:

让我们先在pom.xml中添加Spring的库依赖。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.1.4.RELEASE</version>         
</dependency>

The latest version of the Spring dependency can be found here.

最新版本的Spring依赖性可以在这里找到。

3. Dependency Injection – an Overview

3.依赖性注入–概述

Dependency injection is a technique whereby dependencies of an object are supplied by external containers.

依赖性注入是一种技术,对象的依赖性是由外部容器提供的。

Let’s say we’ve got an application class that depends on a service that actually handles the business logic:

假设我们有一个应用类,它依赖于一个实际处理业务逻辑的服务。

public class IndexApp {
    private IService service;
    // standard constructors/getters/setters
}

Now let’s say IService is an Interface:

现在我们说IService是一个接口。

public interface IService {
    public String serve();
}

This interface can have multiple implementations.

这个接口可以有多个实现。

Let’s have a quick look at one potential implementation:

让我们快速看一下一个潜在的实施方案。

public class IndexService implements IService {
    @Override
    public String serve() {
        return "Hello World";
    }
}

Here, IndexApp is a high-level component that depends on the low-level component called IService.

这里,IndexApp是一个高层组件,它依赖于名为IService的低层组件。

In essence, we’re decoupling IndexApp from a particular implementation of the IService which can vary based on the various factors.

实质上,我们正在将IndexAppIService的特定实现解耦,该实现可根据各种因素而变化。

4. Dependency Injection – in Action

4.依赖性注入–在行动中

Let’s see how we can inject a dependency.

让我们看看如何注入一个依赖关系。

4.1. Using Properties

4.1.使用属性

Let’s see how we can wire the dependencies together using an XML-based configuration:

让我们看看我们如何使用基于XML的配置将这些依赖关系连接起来。

<bean 
  id="indexService" 
  class="com.baeldung.di.spring.IndexService" />
     
<bean 
  id="indexApp" 
  class="com.baeldung.di.spring.IndexApp" >
    <property name="service" ref="indexService" />
</bean>    

As can be seen, we’re creating an instance of IndexService and assigning it an id. By default, the bean is a singleton. Also, we’re creating an instance of IndexApp.

可以看出,我们正在创建一个IndexService的实例,并给它分配一个id。默认情况下,这个Bean是一个单子。此外,我们正在创建一个IndexApp的实例。

Within this bean, we’re injecting the other bean using setter method.

在这个Bean中,我们使用setter方法来注入另一个Bean。

4.2. Using Constructor

4.2.使用构造函数

Instead of injecting a bean via the setter method, we can inject the dependency using the constructor:

我们可以使用构造函数来注入依赖关系,而不是通过setter方法来注入Bean。

<bean 
  id="indexApp" 
  class="com.baeldung.di.spring.IndexApp">
    <constructor-arg ref="indexService" />
</bean>    

4.3. Using Static Factory

4.3.使用静态工厂

We can also inject a bean returned by a factory. Let’s create a simple factory that returns an instance of IService based on the number supplied:

我们也可以注入一个由工厂返回的bean。让我们创建一个简单的工厂,根据提供的数字返回一个IService的实例。

public class StaticServiceFactory {
    public static IService getNumber(int number) {
        // ...
    }
}

Now let’s see how we could use above implementation to inject a bean into IndexApp using an XML-based configuration:

现在让我们看看如何使用上述实现,使用基于XML的配置将Bean注入到IndexApp

<bean id="messageService"
  class="com.baeldung.di.spring.StaticServiceFactory"
  factory-method="getService">
    <constructor-arg value="1" />
</bean>   
  
<bean id="indexApp" class="com.baeldung.di.spring.IndexApp">
    <property name="service" ref="messageService" />
</bean>

In the above example, we’re calling the static getService method using factory-method to create a bean with id messageService which we inject into IndexApp.

在上面的例子中,我们使用factory-method调用静态的getService方法来创建一个id为messageService的Bean,我们将其注入到IndexApp.

4.4. Using Factory Method

4.4.使用工厂方法

Let’s consider an instance factory that returns an instance of IService based on the number supplied. This time, the method is not static:

让我们考虑一个实例工厂,它根据提供的数字返回一个IService的实例。这一次,该方法不是静态的。

public class InstanceServiceFactory {
    public IService getNumber(int number) {
        // ...
    }
}

Now let’s see how we could use above implementation to inject a bean into IndexApp using XML configuration:

现在让我们看看如何使用上述实现,使用XML配置将Bean注入到IndexApp

<bean id="indexServiceFactory" 
  class="com.baeldung.di.spring.InstanceServiceFactory" />
<bean id="messageService"
  class="com.baeldung.di.spring.InstanceServiceFactory"
  factory-method="getService" factory-bean="indexServiceFactory">
    <constructor-arg value="1" />
</bean>  
<bean id="indexApp" class="com.baeldung.di.spring.IndexApp">
    <property name="service" ref="messageService" />
</bean>

In the above example, we’re calling the getService method on an instance of InstanceServiceFactory using factory-method to create a bean with id messageService which we inject in IndexApp.

在上面的例子中,我们使用factory-methodInstanceServiceFactory的一个实例上调用getService方法,以创建一个id为messageService的bean,我们将其注入到IndexApp

5. Testing

5.测试

This is how we can access configured beans:

这就是我们如何访问配置的Bean。

@Test
public void whenGetBeans_returnsBean() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("...");
    IndexApp indexApp = applicationContext.getBean("indexApp", IndexApp.class);
    assertNotNull(indexApp);
}

6. Conclusion

6.结论

In this quick tutorial, we illustrated examples of how we can inject dependency using the XML-based configuration using Spring Framework.

在这个快速教程中,我们举例说明了如何使用Spring框架的基于XML的配置来注入依赖性。

The implementation of these examples can be found in the GitHub project – this is a Maven-based project, so it should be easy to import and run as-is.

这些示例的实现可以在GitHub项目中找到–这是一个基于Maven的项目,所以应该很容易导入并按现状运行。