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
- https://docs.spring.io/spring-boot/reference/using/using-the-springbootapplication-annotation.html
- Starting point of spring boot app
- acts as a configuration file
- starting point of component scan
- initiates auto configuration that is part of spring boot itself
- 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
@ComponentScanto automatically pick up all Spring components, including@Configurationclasses.
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
- ????
