Main Method | Spring Boot Bootstrapping

Learn via video courses
Topics Covered

Overview

As spring boot application creates an executable JAR with all the dependencies built in. Therefore there should be an entry point into the jar for it to execute. Spring boot provides entry into the application using the main method.

What is the @SpringBootApplication

If we decompile the source of this annotation, we can see the following snippets.

@SpringBootApplication is a combination of the following three annotations.

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan

@SpringBootConfiguration

This annotation is the same as the @Configuration annotation. It designates the class as a configuration class.

@ComponentScan

This annotation enables component scanning. Any bean available in the classpath will be scanned by the Spring Framework and registered in the IoC container. For example, classes marked with @Component, @RestController, and @Service will be registered with the Spring IoC container.

@EnableAutoConfiguration

This annotation enables the magical auto-configuration feature of Spring Boot. Without this annotation, spring boot loses its power, and the framework needs to be configured by developers for everything like dispatcher servlet, entity factory, etc.

If you individually replace @SpringBootApplicaiton with the above three annotations, the effect will be the same. @SpringBootApplication annotation is provided only for developer convenience. It does not have any special logic tagged to it.

Parameters Accepted in the @SpringBootApplication annotation

SpringBootApplication annotation accepts parameters to customize the application. Parameters are allies for individual annotation. A few key parameters are:

exclude()

This parameter allows developers to exclude specific auto-configurations. For example, if you don't want the framework-configured data source, then the required exclusion should be supplied to the parameter.

OR

scanBasePackages()

By default, spring boot scans the current package and all the child packages for spring beans. This parameter allows developers to override default scan behavior. We can specify what packages our application should specifically scan for the spring beans.

OR

What is the Role of @SpringBootApplication Annotation?

The Class with the main method in spring boot must be annotated with the @SpringBootApplication annotation. It's a bootstrap class for any spring boot application, and this annotation during the bootstrapping adds all the framework capabilities like enabling autoconfiguration, scanning packages for spring beans, etc.

Conclusion

  • @SpringBootApplication is a stereotype annotation.
  • Class with main method must be annotated with @SpringBootApplication annotation.
  • @SpringBootApplication annotation comprises three individual annotations. This annotation is provided for developer convenience.
  • main method and @SpringBootApplication provided entry points in the bootable jar and bootstrap the application.