Spring Boot - Auto Configuration with @EnableAutoConfiguration

[Updated: Dec 6, 2016, Created: Dec 4, 2016]

Using @EnableAutoConfiguration is like using @Configuration annotation. It configures and wires our beans based on what @Bean methods we have defined in our configuration class. It does other additional configurations too, which includes configuring/invoking helper components (like embedded tomcat in web application). This mechanism works based on the jar dependencies available in the classpath. The jar are usually supplied via starter dependencies, but that's not the absolute requirement. The two things (starters and auto-configuration) are not dependent on each others.

Example using @EnableAutoConfiguration

In this example we are going to use spring-boot-starter-web which will add Spring MVC and Tomcat dependencies. Because of these dependencies auto-configuration mechanism will assume that we are developing a web application and setup Spring accordingly.

<project ...>
public class MyBean {
   public String getMessage () {
       return "a message from MyBean";
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class MyWebController {

   private MyBean myBean;

   public String theHandler () {
       return myBean.getMessage();

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;

public class EnabledAutoConfigExample {

   public MyBean myBean () {
       return new MyBean();

   public MyWebController controller () {
       return new MyWebController();

   public static void main (String[] args) {
       SpringApplication.run(EnabledAutoConfigExample.class, args);

Output in browser

In the next tutorial we will see how to do component scanning with @EnableAutoConfiguration.

Example Project

Dependencies and Technologies Used :

  • Spring Boot Web Starter 1.4.2.RELEASE: Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
    Corresponding Spring version: 4.3.4.RELEASE
  • JDK 1.8
  • Maven 3.3.9

Enable Auto Config Select All Download
  • spring-enable-auto-config
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also