DEV Community

Salad Lam
Salad Lam

Posted on

1

Spring Boot: About @SpringBootApplication

Following is the definition. For following discussion assumes that the class com.example.app.TestApplication annotated with @SpringBootApplication.

@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 {
  // ...
}
Enter fullscreen mode Exit fullscreen mode

@EnableAutoConfiguration annotation

@EnableAutoConfiguration means to lookup org.springframework.boot.autoconfigure.EnableAutoConfiguration entry from \META-INF\spring.factories file of all jar files. Following is one of the entries.

# spring-boot-autoconfigure-2.6.11.jar!\META-INF\spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
# ...
Enter fullscreen mode Exit fullscreen mode

Specified AutoConfiguration classes coming from different files will be merged, imported to ApplicationContext and processed after finishing processing @Configuration classes. Spoken behaviour is controlled by org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.

@AutoConfigurationPackage annotation

@AutoConfigurationPackage is annotated in @EnableAutoConfiguration. This is for saving the path of base package (in this example com.example.app) and allow other configuration class (such as Spring Data) to scan annotation which is not handled by @ComponentScan. (In case of Spring Data, @Repository. Although it is annotated with @Component, but @Repository is annotated in interface that will not be processed by @ComponentScan.)

@SpringBootConfiguration annotation

Same behaviour as @Configuration, in addition that any properties defined will be merged into test class and will not be affected by the existence of @AutoConfigureXXX annotation.

@ComponentScan annotation

Scan component from base package (in this example com.example.app) and all childs. Exclude AutoConfiguration class found by @EnableAutoConfiguration (controlled by org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter) and child filter of TypeExcludeFilter (controlled by org.springframework.boot.context.TypeExcludeFilter).

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs