Spring Boot
applicationContext.xml
XML Configuration
Java
Spring Framework

Spring-boot automatically import applicationContext.xml?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Spring Boot does not automatically import applicationContext.xml just because the file exists on the classpath. Boot strongly prefers Java configuration, component scanning, and externalized properties. If you want XML bean definitions loaded, you must opt in explicitly, usually with @ImportResource.

What Spring Boot Does By Default

A typical Spring Boot application starts from a class annotated with @SpringBootApplication. That enables:

  • component scanning
  • auto-configuration
  • Java-based configuration processing

What it does not do is scan the classpath for applicationContext.xml and load it automatically the way older XML-centric Spring applications often did.

So the direct answer is:

  • no, Boot does not auto-import applicationContext.xml

The Standard Way To Load XML In Boot

If you still need XML bean definitions, import them explicitly.

java
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.context.annotation.ImportResource;
4
5@SpringBootApplication
6@ImportResource("classpath:applicationContext.xml")
7public class DemoApplication {
8    public static void main(String[] args) {
9        SpringApplication.run(DemoApplication.class, args);
10    }
11}

That tells Spring to load bean definitions from the XML file during application startup.

If the XML file lives elsewhere, adjust the resource path accordingly.

Example XML File

A minimal applicationContext.xml might look like this:

xml
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4       xsi:schemaLocation="http://www.springframework.org/schema/beans
5       http://www.springframework.org/schema/beans/spring-beans.xsd">
6
7    <bean id="messageService" class="com.example.demo.MessageService" />
8</beans>

With @ImportResource in place, that bean becomes part of the application context like any other Spring-managed bean.

When XML Still Makes Sense

XML is no longer the default style in Spring Boot, but it still appears in real projects:

  • migration of legacy Spring applications
  • reuse of old bean definitions that are already stable
  • integration with older framework modules or vendor examples

In those cases, Boot supports XML fine. It just does not assume XML should be loaded automatically.

Mixing XML And Java Config

It is perfectly valid to mix XML configuration with modern Boot configuration. For example:

  • controllers discovered by component scanning
  • properties in application.yml
  • a few legacy infrastructure beans in XML

This is often a good migration strategy because it lets you move incrementally instead of rewriting the whole configuration model at once.

When Not To Use applicationContext.xml

If you are starting a new Boot application from scratch, XML is rarely the best choice. Annotation-based configuration and explicit @Bean methods are usually easier to refactor, test, and navigate.

For example, a Java configuration class often replaces a simple XML bean cleanly:

java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3
4@Configuration
5public class AppConfig {
6    @Bean
7    public MessageService messageService() {
8        return new MessageService();
9    }
10}

Boot is built around this style.

Common Reasons People Think XML Is Auto-Loaded

This confusion usually comes from older Spring habits. In classic Spring applications, XML was often the primary configuration mechanism, so developers expect applicationContext.xml to have special meaning automatically.

In Spring Boot, the framework entry point is different. Boot creates the context from the application class and its configuration model, not from a magical XML filename convention.

Common Pitfalls

  • Putting applicationContext.xml on the classpath and expecting Boot to discover it automatically.
  • Using the wrong resource path in @ImportResource.
  • Mixing XML and Java configuration without realizing two beans of the same type may now exist.
  • Keeping large XML configuration files around in new projects when a small Java config class would be clearer.
  • Assuming Boot dislikes XML entirely. It supports XML, but only when you opt in.

Summary

  • Spring Boot does not automatically import applicationContext.xml.
  • Use @ImportResource("classpath:applicationContext.xml") when you want XML bean definitions loaded.
  • XML support is useful for legacy migration and mixed-configuration applications.
  • New Boot applications usually benefit more from Java configuration and annotations.
  • If XML is not loading, the first thing to check is whether you explicitly imported it at all.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.