Spring boot Annotations

  • Java annotations are just metadata and read by annotation processors
  • They are not decorators

Common Examples:

@Bean @Service stereotype around @Component @Component @ComponentScan @SpringBootTest @SpringBoot @Configuration @ConfigurationProperties @EnableConfigurationProperties @SpringBootApplication @Repository @Controller @RestController @Transactional

@SpringBootApplication

  • This annotation combines following annotations with their default attributes:
    • @EnableAutoConfiguration
      • enable Spring Boot’s auto-configuration mechanism
    • @ComponentScan
      • scan on the package for @Component
    • @SpringBootConfiguration
      • enable registration of extra beans in the context or the import of additional configuration classes.
      • Similar to @Configuration

Stereotype Annotations

  • Stereotype annotations specialized annotations that are used to indicate the role or purpose of a particular component
  • examples:
    • @Component (main stereotype annotation)
      • @Service (indicate Service)
      • @Repository (indicate repository)
      • @Controller (indicate Controller)
      • @Configuration

@Import

  • used to import additional configuration classes.
  • Alternatively, you can use @ComponentScan to automatically pick up all Spring components, including @Configuration classes.

More annotations

  • @Import
  • @ComponentScan
  • @PropertySource
  • @Order
@Configuration
@EnableAutoConfiguration
@Import({
        BeaconWebConfig.class,
        BeaconServiceWebConfigCustomizer.class,
        BeaconHealthIndicatorConfig.class
})
@ComponentScan({
        "com.xyz.beacon.controllers",
        "com.xyz.beacon.legacy.controllers",
})
@PropertySource(value = "classpath:beacon-web-config.yaml", factory = BeaconInternalYamlPropertySourceFactory.class)
@Order(1)
public class BeaconServiceWebConfig implements WebMvcConfigurer, ApplicationContextAware {}

Custom Annotations and practical example

  • Annotation Processor
  • ????

spring_boot_annotations