Close

Spring Boot - Auto-configuration with @SpringBootApplication

[Last Updated: Oct 16, 2018]

Since @EnableAutoConfiguration and @ComponentScan are likely to be frequently used by the developers, Spring Boot provides a convenient equivalent shortcut (a meta-annotation) for these two annotations: @SpringBootApplication.


Example using @SpringBootApplication

<project ...>
  ....
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.2.RELEASE</version>
 </parent>
   ....
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
 </dependencies>
</project>
@Component
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;

@Controller
public class MyWebController {

   @Autowired
   private MyBean myBean;

   @RequestMapping("/")
   @ResponseBody
   public String theHandler () {
       return myBean.getMessage();
   }
}

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

@SpringBootApplication
public class EnabledAutoConfigExample {

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

Output in browser


Just like @ComponentScan elements: basePackageClasses and basePackages, @SpringBootApplication annotation provides scanBasePackageClasses and scanBasePackages to be specified if the scan base path is not same as of this configuration class.





Example Project

Dependencies and Technologies Used:

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

Spring Boot Annotation Select All Download
  • spring-boot-annotation
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • EnabledAutoConfigExample.java

    See Also