Friday 20 July 2018

Spring boot: @SpringBootApplication annotation

If you are working with spring boot application, then you should be aware of @SpringBootApplication.

What exactly @SpringBootApplication offers?
When you see the source code of @SpringBootApplication annotation. it looks like below.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
                  @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
                  @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

}

Since @SpringBootApplication annotation is annotated with @SpringBootConfiguration, @SpringBootConfiguration and @ComponentScan, it will provide all the features that are provided by these 3 annotations.

By placing @SpringBootApplication on the application class, spring takes care of component scanning, beans handling and auto configuration of spring application.

package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

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

  /* Do your logic here */

  applicationContext.close();
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment