Do you prefer using XML or annotations for configuration in Spring applications, and why?

In modern Spring applications, annotations are generally preferred over XML configuration because they provide a cleaner, more concise, and type-safe way to configure components directly within the code. Annotations reduce configuration overhead, improve readability, and make applications easier to maintain, especially in large projects.

Key Points

Annotations vs XML

Annotations

XML

Example

Using annotations, a service bean can be registered directly within the class instead of defining it separately in an XML file.

@Service
public class PaymentService {

    public void processPayment() {

        System.out.println(
                "Payment Processed");
    }
}

Equivalent XML Configuration

<bean id="paymentService"

class="com.app.service.PaymentService"/>

Why Annotations Are Preferred

When XML May Still Be Useful

Real-World Practice

In most modern Spring Boot projects, annotations such as @Component, @Service, @Repository, @Controller, and @Configuration are used extensively, while XML configuration is rarely required.

Interview Tip

A concise interview answer is: I generally prefer annotations because they reduce boilerplate code, improve readability, and keep configuration close to the implementation. They are the standard approach in Spring Boot applications. However, XML can still be useful in legacy systems or when configuration needs to be managed externally without changing the application code.

Last updated: 16 July 2026

Previous question: What happens if multiple AutoConfiguration classes define the same bean?

Next question: What is the difference between the @Spy and @Mock annotations in Mockito?

More Spring Framework - Level II interview questions

All Spring Framework - Level II questions