Spring Boot @EnableAutoConfiguration Example

1. Introduction

@EnableAutoConfiguration in Spring Boot is a key annotation that tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. It is part of the convention over the configuration paradigm that Spring Boot adopts to reduce developer effort in setting up an application.

Key Points:

1. @EnableAutoConfiguration attempts to automatically configure your Spring application based on the JAR dependencies you added.

2. It is normally used with @Configuration classes to have fine-grained control over the application context.

3. This annotation can be used to automatically configure certain features, such as web applications, data sources, message brokers, etc.

2. Implementation Steps

1. Initialize a new Spring Boot project with the required dependencies.

2. Create a main application class and annotate it with @Configuration and @EnableAutoConfiguration.

3. Optionally, exclude certain auto-configurations if they are not desirable.

4. Create a REST controller to expose an endpoint.

5. Run the main class to start the application and see the results.

3. Implementation Example

Here is the complete code that demonstrates the usage of @EnableAutoConfiguration annotation:
// Step 2: Define the main application class with Configuration and EnableAutoConfiguration
@Configuration
@EnableAutoConfiguration
public class AutoConfigApplication {

    // Step 4: Create a REST controller
    @RestController
    class SimpleController {
        @GetMapping("/")
        public String home() {
            return "Welcome to auto-configured Spring Boot!";
        }
    }

    // Step 5: Run the application main method
    public static void main(String[] args) {
        SpringApplication.run(AutoConfigApplication.class, args);
    }
}

@Configuration
// Step 3: Optionally exclude a class from auto-configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class AutoConfigApplication {
}

Run:

mvn spring-boot:run

Output:

When the application runs, navigating to http://localhost:8080 will display:

Welcome to auto-configured Spring Boot!

Explanation:

1. The class AutoConfigApplication is annotated with @Configuration making it a source of bean definitions.

2. @EnableAutoConfiguration tells Spring Boot to guess and configure beans that you are likely to need.

3. SimpleController is a class marked with @RestController, making it a web controller that responds to web requests.

4. @GetMapping("/") defines that the home method should handle HTTP GET requests to the root path.

5. When AutoConfigApplication runs, Spring Boot auto-configures a web server (like Tomcat or Jetty) because it sees spring-webmvc on the classpath.

6. Visiting the root URL, you get a response from SimpleController, which is possible due to the automatic configuration set up by Spring Boot.

7. If there are certain auto-configurations that you don't want to apply, such as DataSourceAutoConfiguration, you can exclude them using the exclude attribute of the @EnableAutoConfiguration annotation.


Comments