Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using the Spring Boot @SpringBootApplication Annotation

DZone's Guide to

Using the Spring Boot @SpringBootApplication Annotation

Want to learn more about using the @SpringBootApplication annotation on your Spring Boot project? Check out this post to learn more.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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 convenient annotation, which is the equivalent of declaring @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Let's look at an internal implementation of the @SpringBootApplication annotation by looking into its source code. The diagram below shows an internal implementation of @SpringBootApplication: SpringBootApplication Annotation

If you have been using Spring Boot for a long time, then you know that earlier we needed to annotate our Application or Main class with a lot of annotations:

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 for their apps to use auto-configuration, a 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, including:

  • @EnableAutoConfiguration: enables Spring Boot’s auto-configuration mechanism
  • @ComponentScan: enables the @Component scan on the package where the application is located
  • @Configuration: allows us 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 = @Configuration + @EnableAutoConfiguration + @ComponentScan

@SpringBootApplication 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 Optional Elements

The following are the parameters accepted with the @SpringBootApplication:

  • Class<?>[] exclude:Exclude specific auto-configuration classes so that they will never be applied.
  • String[] excludeName: Exclude specific auto-configuration class names so 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 the @SpringBootApplication annotation is equivalent to using the @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).

You may also be interested to know more about the @EnableAutoConfigurationannotation.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
spring boot ,spring annotations ,java ,springbootapplication ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}