Generic Top

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE

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.

Generic bottom

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE
newest oldest most voted
Notify of
Abhishek Belgundi
Guest
Abhishek Belgundi

Hi,
I would like to know more about the below sentence with an example.

“The main difference is that @SpringBootConfiguration allows configuration to be automatically located.”

Eric Martin
Member
Eric Martin

Hey Abhishek,
It’s not easy to provide an example, but I can clarify: @SpringBootConfiguration is functionally equivalent to @Configuration; however, certain components of Spring Boot look for the former and not the latter, in particular, the @SpringBootTest annotation used for integration tests.
The algorithm that Spring Boot uses to auto-detect the configuration to use for a particular integration test is detailed at:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-detecting-config.

Comments are closed on this article!