Repaso
EspaƱol

What is the purpose of @SpringBootApplication?

Options

  1. It starts the database that the application will use
  2. It combines configuration, auto-config and component scan (Correct option)
  3. It defines a REST endpoint directly on the main class
  4. It runs the unit tests when the application starts

Answer

It combines @Configuration, @EnableAutoConfiguration and @ComponentScan, marking the main class of the app

Why

Placed on your main class, it is a shortcut for three annotations:

  • @Configuration: the class can define beans.
  • @EnableAutoConfiguration: turn on Spring Boot's automatic setup.
  • @ComponentScan: scan this package and sub-packages for @Component, @Service...

That is why the main class should live in the top-level package of your project.

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

Remember

Main class annotation = config + auto-config + component scan.

Key terms

Practice this question in Repaso