Usage of @SpringBootApplication in SpringBoot Application with Example

 If we define the annotation @SpringBootApplication, the compiler marks the main class as Spring Boot Configuration Class and does all auto configurations and other related services required for SpringBoot.

 Generally, @SpringBootApplication is equivalent with three other annotations namely @Configuration, @EnableAutoConfiguration, and @ComponentScan

  1. @Configuration – Used to enable Java-based configuration
  2.  @ComponentScan – Used to enable component scanning.
  3.  @EnableAutoConfiguration – Used to enable Auto-configuration feature of Spring Boot.

So what are the main differences between these Annotations?

@Configuration :
If a class is mentioned with @Configuration annotation, it informs the spring container like the class contains one or more @bean methods, these beans will be processed as bean definition during runtime.

com.java
@Configuration
public class MyBeanConfig {
@Bean(name="demoService")
public DemoClass service() 
{

}
}

@ComponentScan :

Scans all the annotated Components available in the entire application. Do the complete Scanning across the project based on the defined dependencies and imports the classes into beans.

@ComponentScan scan detect and register the beans with the classes annotated with @Component, @controller, @service, @Configuration.

@EnableAutoConfiguration :

EnableAutoConfiguration annotation enables all the necessary configurations required for your application configured in your dependencies.

Before Spring Boot 1.2, To implement the Spring Boot, we need to configure these 3 annotations as the basic setup for the application, Since the comfortable use above 3 configurations in the application, From Spring Boot 1.2, The Annotation named @SpringBootApplication introduces and takes care of all the configurations and parameters required for the Main Class.

Below is the Configuration Example to define @SpringBootApplication

package com.example.common;
@SpringBootApplication
public class LocalApplication {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

Conclusion :

Well, Thats end of the @SpringBootApplication annotation, Read more related topics provided below.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *