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.
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.
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:
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:
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.xmlon 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
- spring-boot default log location
- Spring-boot default profile for integration tests
- Spring-boot default profile for integration tests
- Spring-Boot execute data.sql in one profile only
- spring-boot Fatal error compiling invalid target release 17
- spring-boot gradle plugin can't be found
- spring-boot health not showing details withDetail info
- Spring-Boot How do I set JDBC pool properties like maximum number of connections?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.