Spring Boot @SpringBootApplication Annotation with Example

In this article, we will discuss the importance of the annotation @SpringBootApplication with an example to get you started. Let's get into it!

@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
If you have been using Spring Boot for a long time then you know that earlier we need to annotate our Application class or Main class with quite a lot of annotations to start with e.g.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Many Spring Boot developers like their apps to use auto-configuration, component scan and be able to define extra configuration on their "application class". A single @SpringBootApplication annotation can be used to enable those three features, that is:
  • @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
  • @ComponentScan: enable @Component scan on the package where the application is located
  • @Configuration: allow to register extra beans in the context or import additional configuration classes
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.


@SpringBootApplication Annotation Example

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);
    }
}

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 by 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, 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