@SpringBootApplication Spring Boot Example

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

YouTube Video

@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 @SpringBootApplication annotation is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration, and @ComponentScan.
 
The below diagram shows an internal implementation of @SpringBootApplication Annotation:
Spring Boot @SpringBootApplication Annotation with Example

Note that the above @SpringBootApplication annotation code internally uses @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations 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 indicates that a configuration class declares one or more @Bean methods. These classes are processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

@ComponentScan

This annotation is used to specify the base packages to scan for spring beans/components.

@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 that @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

Related Spring and Spring Boot Annotations

Comments