1. Overview

In this tutorial, we’ll briefly discuss the @SpringBootConfiguration annotation. We’ll also look at its usage in a Spring Boot application.

2. Spring Boot Application Configuration

@SpringBootConfiguration is a class-level annotation that is part of the Spring Boot framework. It indicates that a class provides application configuration.

Spring Boot favors Java-based configuration. As a result, the @SpringBootConfiguration annotation is the primary source for configuration in applications. Generally, the class that defines the main() method is a good candidate for this annotation.

2.1. @SpringBootConfiguration

Most Spring Boot use @SpringBootConfiguration via @SpringBootApplication, an annotation that inherits from it. If an application uses @SpringBootApplication, it is already using @SpringBootConfiguration.

Let’s look at @SpringBootConfiguration’s usage in an application.

First, we create an application class that contains our configuration:

@SpringBootConfiguration
public class Application {

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

    @Bean
    public PersonService personService() {
        return new PersonServiceImpl();
    }
}

The @SpringBootConfiguration annotation annotates the Application class. This indicates to the Spring container that the class has @Bean definition methods. In other words, it contains methods that instantiate and configure our dependencies.

For example, the Application class contains the bean definition method for the PersonService bean.

Furthermore, the container processes the configuration class. This, in turn, generates beans for the application. As a result, we can now use Dependency Injection annotations like @Autowired or @Inject.

2.2. @SpringBootConfiguration vs @Configuration

@SpringBootConfiguration is an alternative to the @Configuration annotation. The main difference is that @SpringBootConfiguration allows configuration to be automatically located. This can be especially useful for unit or integration tests.

The recommendation is to only have one @SpringBootConfiguration or @SpringBootApplication for your application. Most applications will simply use @SpringBootApplication.

3. Conclusion

In this article, we got a quick look at the @SpringBootConfiguration annotation. Further, we looked at @SpringBootConfiguration‘s usage in a Spring Boot Application. We also reviewed Spring’s @Bean annotation*.*

The full source code of our examples here is, as always, over on GitHub.