DEV Community

Hunor Vadasz-Perhat
Hunor Vadasz-Perhat

Posted on

spring-009: configuration-with-two-out-of-the-box-solutions

Spring Boot Addresses the Configuration Problem with Two Out-of-the-Box Solutions

Spring Boot recognizes that different projects have different configuration needs, so it provides two approaches:

  1. Manual Configuration (@SpringBootConfiguration) – For projects that require fine-grained control.
  2. Automatic Configuration (@EnableAutoConfiguration) – For projects that benefit from convention over configuration (i.e., fewer manual settings).

The choice depends on the specific needs of your project.


When to Use Each Approach?

Use Case Approach Why?
You want full control over beans and configuration @SpringBootConfiguration You define everything manually (like in classic Spring projects).
You want Spring Boot to configure common components automatically @EnableAutoConfiguration It saves time by auto-configuring beans based on dependencies.
You need a mix of both Both Auto-configuration is enabled, but manual configurations override defaults.
You want to test a Spring Boot app with specific configurations @SpringBootConfiguration Useful for unit testing with custom test configurations.
You need to disable or tweak auto-configuration @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) You can disable specific auto-configurations when needed.

Practical Scenarios

1️⃣ Full Manual Configuration (Traditional Spring)

If your project requires full control (e.g., a legacy migration or a highly customized system):

@SpringBootConfiguration
public class MyAppConfig {
    @Bean
    public DataSource dataSource() {
        return new HikariDataSource(); // Manually configured database
    }
}
Enter fullscreen mode Exit fullscreen mode
  • No auto-configuration: You must define all beans manually.
  • Useful when you want to avoid hidden behavior.

2️⃣ Fully Automatic Configuration (Spring Boot Default)

If you want to let Spring Boot handle everything, use:

@SpringBootApplication // Includes @EnableAutoConfiguration
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Auto-configures database, web server, security, etc.
  • Great for fast development and microservices.

3️⃣ Hybrid Approach (Auto + Manual Configuration)

If you need auto-configuration but want to override certain parts, do this:

@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

@Configuration
class ManualConfig {
    @Bean
    public DataSource dataSource() {
        return new HikariDataSource(); // Overrides default auto-configured DataSource
    }
}
Enter fullscreen mode Exit fullscreen mode
  • The manual bean (dataSource) takes precedence over the auto-configured one.
  • Useful when auto-configuration is mostly fine, but you need to customize key parts.

Final Thoughts

Yes, Spring Boot offers two solutions to the configuration problem, and the best choice depends on your project's needs:

  • For fast development, use @EnableAutoConfiguration.
  • For full control, use @SpringBootConfiguration.
  • For a balanced approach, let auto-configuration handle most things, but override when necessary. 🚀

Top comments (0)