Component

During startup, Spring instantiates objects and adds them to the application context. Objects in the application context are called “Spring beans” or “components”.

Component Scanning

The process of searching the classpath for classes that should contribute to the application context is called component scanning.

Stereotype

What is a spring stereotype

  • JavaDoc
    • Annotations denoting the roles of types or methods in the overall architecture (at a conceptual, rather than implementation, level).
  • Merriam-Webster
    • something conforming to a fixed or general pattern; especially : a standardized mental picture that is held in common by members of a group and that represents an oversimplified opinion, prejudiced attitude, or uncritical judgment

Stereotype Annotations In Spring: @Component, @Controller, @Service, @Repository

How component scanning works

Spring Boot’s @SpringBootApplication annotation implies the @Configuration, @ComponentScan, and @EnableAutoConfiguration annotations.

With Spring, we use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned.

  • @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 {}

Component Scanning is worked by @SpringBootConfiguration and @ComponentScan on sub-packages with applied @SpringBootApplication