原文链接:
XML-Based Injection in Spring
1. Introduction 概述
In this basic tutorial, we’ll learn how to do simple XML-based bean configuration with the Spring Framework.
这里,一起学习如何在Spring框架中使用XML文件进行bean对象配置吧
2. Overview 回顾一下
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>
3. Dependency Injection – an Overview 复习下依赖注入
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:
让我们来假设,已经有一个程序类,它依赖了一个service来真正的处理业务逻辑,如下:
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.
大体上来说,我们把IndexApp类与它的某一个特定的实现解耦了。
4. Dependency Injection – in Action 行动起来,上手配置
Let’s see how we can inject a dependency. 来学习如何注入依赖
4.1. Using Properties 属性字段注入
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,默认情况下,它是单例的。同样的,也创建了一个IndexApp的对象。
Within this bean, we’re injecting the other bean using setter method.
通过上面的配置,就可以通过set方法注入依赖的对象了
4.2. Using Constructor 构造函数注入
Instead of injecting a bean via the setter method, we can inject the dependency using the constructor:
除了通过set方法,我们还可以使用构造函数来注入对象所需的依赖,如下:
<bean
id="indexApp"
class="com.baeldung.di.spring.IndexApp">
<constructor-arg ref="indexService" />
</bean>
4.3. Using Static Factory 静态工厂注入
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:
使用工厂也可以实现依赖注入,下面是简单工厂模式中,根据参数来返回所需的IService对象:
public class StaticServiceFactory {
public static IService getService(int number) {
// ...
}
}
Now let’s see how we could use above implementation to inject a bean into
IndexApp
using an XML-based configuration:
下面是XML中的工厂类配置,通过下面配置,可以为IndexApp类提供所需的Service对象
<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.
在上面的示例中,会通过调用getService这个静态方法,并传入特定参数来创建一个bean对象,用它来注入到IndexApp中作为依赖对象。
4.4. Using Factory Method 工厂方法注入
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配置文件中,使用上面的对象,注入IService对象到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
.
在上面的配置示例中,通过InstanceServiceFactory的实例调用了getService工厂方法来创建了一个messageService对象,用它来注入到IndexApp中作为依赖服务使用。
5. Testing
This is how we can access configured beans:
下面是使用XML配置的代码,注意,这里使用了ClassPathXmlApplicationContext
@Test
public void whenGetBeans_returnsBean() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("...");
IndexApp indexApp = applicationContext.getBean("indexApp", IndexApp.class);
assertNotNull(indexApp);
}
6. Conclusion 最后
XML属于Spring框架中,依赖配置的最基本的方式,值得研究学习一下
大家多多点赞关注哈