XML Defined Beans in Spring Boot – Spring Boot中的XML定义的Bean

最后修改: 2020年 8月 3日

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

1. Introduction

1.绪论

Before Spring 3.0, XML was the only way to define and configure beans. Spring 3.0 introduced JavaConfig, allowing us to configure beans using Java classes. However, XML configuration files are still used today.

在Spring 3.0之前,XML是定义和配置Bean的唯一方式。Spring 3.0引入了JavaConfig,允许我们使用Java类来配置bean。然而,XML配置文件至今仍被使用。

In this tutorial, we’ll discuss how to integrate XML configurations into Spring Boot.

在本教程中,我们将讨论如何将XML配置融入Spring Boot

2. The @ImportResource Annotation

2.@ImportResource Annotation

The @ImportResource annotation allows us to import one or more resources containing bean definitions.

@ImportResource注解允许我们导入一个或多个包含Bean定义的资源。

Let’s say we have a beans.xml file with the definition of a bean:

假设我们有一个beans.xml文件,里面有一个bean的定义。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <bean class="com.baeldung.springbootxml.Pojo">
        <property name="field" value="sample-value"></property>
    </bean>
</beans>

To use it in a Spring Boot application, we can use the @ImportResource annotation, telling it where to find the configuration file:

要在Spring Boot应用程序中使用它,我们可以使用@ImportResourceannotation,告诉它在哪里找到配置文件。

@Configuration
@ImportResource("classpath:beans.xml")
public class SpringBootXmlApplication implements CommandLineRunner {

    @Autowired 
    private Pojo pojo;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootXmlApplication.class, args);
    }
}

In this case, the Pojo instance will be injected with the bean defined in beans.xml.

在这种情况下,Pojo实例将被注入beans.xml中定义的bean。

3. Accessing Properties in XML Configurations

3.访问XML配置中的属性

What about using properties in XML configuration files? Let’s say we want to use a property declared in our application.properties file:

那么在 XML 配置文件中使用属性呢?假设我们想使用我们的application.properties文件中声明的一个属性。

sample=string loaded from properties!

Let’s update the Pojo definition, in beans.xml, to include the sample property:

让我们更新Pojo定义,在beans.xml中,以包括sample属性。

<bean class="com.baeldung.springbootxml.Pojo">
    <property name="field" value="${sample}"></property>
</bean>

Next, let’s verify if the property is properly included:

接下来,让我们验证一下该财产是否被正确包含。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootXmlApplication.class)
public class SpringBootXmlApplicationIntegrationTest {

    @Autowired 
    private Pojo pojo;
    @Value("${sample}") 
    private String sample;

    @Test
    public void whenCallingGetter_thenPrintingProperty() {
        assertThat(pojo.getField())
                .isNotBlank()
                .isEqualTo(sample);
    }
}

Unfortunately, this test will fail because, by default, the XML configuration file can’t resolve placeholders. However, we can solve this by including the @EnableAutoConfiguration annotation:

不幸的是,这个测试会失败,因为,默认情况下,XML配置文件不能解决占位符。然而,我们可以通过包括@EnableAutoConfiguration注解来解决这个问题。

@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:beans.xml")
public class SpringBootXmlApplication implements CommandLineRunner {
    // ...
}

This annotation enables auto-configuration and attempts to configure beans.

该注释启用了自动配置功能,并尝试配置Bean。

4. Recommended Approach

4.建议采取的方法

We can continue using XML configuration files. But we can also consider moving all configuration to JavaConfig for a couple of reasons. First, configuring the beans in Java is type-safe, so we’ll catch type errors at compile time. Also, XML configuration can grow quite large, making it difficult to maintain.

我们可以继续使用 XML 配置文件。但我们也可以考虑将所有的配置转移到JavaConfig,原因有二。首先,用Java配置Bean是类型安全的,所以我们可以在编译时捕获类型错误。另外,XML配置可能会变得相当大,使其难以维护。

5. Conclusion

5.总结

In this article, we saw how to use XML configuration files to define our beans in a Spring Boot application. As always, the source code of the example we used is available over on GitHub.

在本文中,我们看到了如何使用 XML 配置文件来定义 Spring Boot 应用程序中的 Bean。一如既往,我们所使用的示例的源代码可在GitHub上获得