@SpringBootApplication annotation in Spring Boot

In this article, we will discuss the @SpringBootApplication annotation. This is the core annotation under use while working on the Spring Boot applications.

 

Introduction

While working on the Spring application, we may use the following 3 annotation frequently.

  1. @Configure
  2. @EnableAutoConfiguration
  3. @ComponentScan

These annotations are the most common while working on any Spring application as they are the building blocks. Based on their frequent use, Spring Boot provides a convenient @SpringBootApplication alternative.

 

1. The @SpringBootApplication

The @SpringBootApplication annotation is a convenience annotation equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan. You can use this annotation in place of using above three different annotations. This is how we use it on our main Spring Boot class:

@SpringBootApplication
public class SpringMvcFileUploadApplication {

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

For simple Spring application, above code look like:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
 @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
 @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)
})
public @interface SpringBootApplication {

 @AliasFor(annotation = EnableAutoConfiguration.class)
 Class <<?> [] exclude() default {};

 @AliasFor(annotation = EnableAutoConfiguration.class)
 String[] excludeName() default {};

 @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
 String[] scanBasePackages() default {};

 @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
 Class <<?> [] scanBasePackageClasses() default {};

}

 

1.1 @Configuration Annotation

Indicates that a class declares one or more @Bean methods and use by the Spring container to generate bean definitions and service requests for those beans at run-time. This is important to support Java-based configuration over XML configuration.

 

1.2 @EnableAutoConfiguration

This enable the Spring Boot auto configuration feature.

 

1.3 @ComponentScan

This annotation enables a component-scanning so that the web controller classes and other components automatically discovered and registered as beans.

 

2. The @SpringBootApplication Parameters

The @SpringBootApplication annotation provides the option to customize its behaviour by passing additional parameters:

  • String[] excludeName –  Exclude specific auto-configuration class names.
  • String[] scanBasePackages – Base packages to scan for annotated components.
  • Class<?>[] scanBasePackageClasses – Type-safe alternative to scanBasePackages for specifying the packages to scan for annotated components.
  • Class<?>[] exclude – Exclude specific auto-configuration classes.

 

Summary

In this sort post, we cover the @SpringBootApplication annotation. We talk about the how this new annotation provide the flexibility and convenience in your Spring Boot application.