@SpringBootApplication Spring Boot Example

In this article, we will discuss one very important Spring Boot annotation that is @SpringBootApplication with an example.

@SpringBootApplication Annotation Overview

@SpringBootApplication annotation indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Below diagram shows an internal implementation of @SpringBootApplication Annotation:
Spring Boot @SpringBootApplication Annotation with Example

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.

@SpringBootApplication Annotation Example

We use this annotation to mark the main class of a Spring Boot application:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.

The @SpringBootApplication annotation is a combination of the following three Spring annotations:


@Configuration

This annotation marks a class as a Configuration class for Java-based configuration.

@ComponentScan

This annotation enables component-scanning so that the web controller classes and other components you create will be automatically discovered and registered as beans in Spring's Application Context. All the@Controller classes you write are discovered by this annotation.

@EnableAutoConfiguration

This annotation enables the magical auto-configuration feature of Spring Boot, which can automatically configure a lot of stuff for you.

SpringBootApplication Annotation Optional Elements

The following are the parameters accepted in the @SpringBootApplication annotation:
  • Class<?>[] exclude - Exclude specific auto-configuration classes such that they will never be applied.
  • String[] excludeName - Exclude specific auto-configuration class names such that they will never be applied.
  • Class<?>[] scanBasePackageClass - A type-safe alternative to scanBasePackages() for specifying the packages to scan for annotated components.
  • String[] scanBasePackages - Base packages to scan for annotated components.

@SpringBootApplication Annotation Features not Mandatory

So far we have seen @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan but none of these features are mandatory and you may choose to replace this single annotation with any of the features that it enables. For instance, you may not want to use a component scan in your application:
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@EnableAutoConfiguration
@Import({ MyConfig.class, MyAnotherConfig.class })
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
In this example, the Application is just like any other Spring Boot application except that @Component annotated classes are not detected automatically and the user-defined beans are imported explicitly (see @Import).

Learn complete Spring boot on Spring Boot Tutorial

Comments