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).

Billboard image

Synthetic monitoring. Built for developers.

Join Vercel, Render, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

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

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay