Enterprise Intermediate

Replace verbose Spring XML bean definitions with concise annotation-driven configuration in Spring Boot.

โœ• Spring (XML)
<!-- applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="userRepository"
          class="com.example.UserRepository">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="userService"
          class="com.example.UserService">
        <property name="repository" ref="userRepository"/>
    </bean>

</beans>
โœ“ Spring Boot 3+
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@Repository
public class UserRepository {
    private final JdbcTemplate jdbc;

    public UserRepository(JdbcTemplate jdbc) {
        this.jdbc = jdbc;
    }
}

@Service
public class UserService {
    private final UserRepository repository;

    public UserService(UserRepository repository) {
        this.repository = repository;
    }
}
See a problem with this code? Let us know.
๐Ÿšซ

No XML

@SpringBootApplication triggers component scanning and auto-configuration, eliminating all XML wiring files.

๐Ÿ’‰

Constructor injection

Spring injects dependencies through constructors automatically, making beans easier to test and reason about.

โšก

Auto-configuration

Spring Boot configures DataSource, JPA, and other infrastructure from the classpath with zero boilerplate.

Old Approach
XML Bean Definitions
Modern Approach
Annotation-Driven Beans
Since JDK
17
Difficulty
Intermediate
Spring XML Bean Config vs Annotation-Driven
Available

Widely available since Spring Boot 1.0 (April 2014); Spring Boot 3 requires Java 17+

Traditional Spring applications wired beans through XML configuration files, declaring each class and its dependencies as verbose <bean> elements. While annotation support existed since Spring 2.5, XML remained the dominant approach until Spring Boot introduced auto-configuration. Spring Boot detects beans annotated with @Component, @Service, @Repository, and @Controller via classpath scanning, satisfies dependencies through constructor injection automatically, and configures infrastructure like DataSource from the classpath โ€” eliminating all XML wiring files.

Share ๐• ๐Ÿฆ‹ in โฌก