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
.
Annotation | Details |
---|---|
@SpringBootApplication | Main spring boot application annotation. used one time in the application, contains a main method, and act as main package for package scanning |
@SpringBootConfiguration | Indicates that a class provides Spring Boot application. Should be declared only once in the application, usually automatically by setting @SpringBootApplication |
@EnableAutoConfiguration | Enable auto-configuration of the Spring Application Context. Should be declared only once in the application, usually automatically by setting @SpringBootApplication |
@ComponentScan | Used to trigger automatic package scanning on a certain package and its children or to set custom package scanning |
@Configuration | Used 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 |
@Bean | Indicates 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. |
@Component | By 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 |
@Repository | Originally defined by Domain-Driven Design (Evans, 2003) as "a mechanism for encapsulating storage. It is usualy used to indicate a Repository for spring data |
@Service | Very 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." |
@Controller | Indicates that an annotated class is a "Controller" (e.g. a web controller). |
@RestController | A 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. |