1. Introduction

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.

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

2. The @ImportResource Annotation

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

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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>

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

public class SpringBootXmlApplication implements CommandLineRunner {

    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.

3. Accessing Properties in XML Configurations

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

sample=string loaded from properties!

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

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

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

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

    private Pojo pojo;
    private String sample;

    public void whenCallingGetter_thenPrintingProperty() {

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:

public class SpringBootXmlApplication implements CommandLineRunner {
    // ...

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

4. Recommended Approach

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.

5. Conclusion

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.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Comments are closed on this article!