spring-boot Package scanning

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

In this topic I will overview spring boot package scanning.

You can find some basic information in spring boot docs in the following link (using-boot-structuring-your-code) but I will try to provide more detailed information.

Spring boot, and spring in general, provide a feature to automatically scan packages for certain annotations in order to create beans and configuration.

Parameters

AnnotationDetails
@SpringBootApplicationMain spring boot application annotation. used one time in the application, contains a main method, and act as main package for package scanning
@SpringBootConfigurationIndicates that a class provides Spring Boot application. Should be declared only once in the application, usually automatically by setting @SpringBootApplication
@EnableAutoConfigurationEnable auto-configuration of the Spring Application Context. Should be declared only once in the application, usually automatically by setting @SpringBootApplication
@ComponentScanUsed to trigger automatic package scanning on a certain package and its children or to set custom package scanning
@ConfigurationUsed to declare one or more @Bean methods. Can be picked by auto package scanning in order to declare one or more @Bean methods instead of traditional xml configuration
@BeanIndicates that a method produces a bean to be managed by the Spring container. Usually @Bean annotated methods will be placed in @Configuration annotated classes that will be picked by package scanning to create java configuration based beans.
@ComponentBy declaring a class as a @Component it becomes a candidates for auto-detection when using annotation-based configuration and classpath scanning. Usually a class annotated with @Component will become a bean in the application
@RepositoryOriginally defined by Domain-Driven Design (Evans, 2003) as "a mechanism for encapsulating storage. It is usualy used to indicate a Repository for spring data
@ServiceVery similar in practice to @Component. originally defined by Domain-Driven Design (Evans, 2003) as "an operation offered as an interface that stands alone in the model, with no encapsulated state."
@ControllerIndicates that an annotated class is a "Controller" (e.g. a web controller).
@RestControllerA convenience annotation that is itself annotated with @Controller and @ResponseBody. Will be automatically picked by default because it contains the @Controller annotation that is picked by default.


Got any spring-boot Question?