参考: https://www.baeldung.com/spring-boot-annotations
@SpringBootApplication
mark the main class of a Spring Boot application:
@SpringBootApplication encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes.
@EnableAutoConfiguration
@EnableAutoConfiguration enables auto-configuration. It means that Spring Boot looks for auto-configuration beans on its classpath and automatically applies them.
we have to use this annotation with @Configuration:
@Configuration @EnableAutoConfiguration class VehicleFactoryConfig {}
Auto-Configuration Conditions
when we write our custom auto-configurations, we want Spring to use them conditionally. We can place the annotations in this section on @Configuration classes or @Bean methods.
@ConditionalOnClass and @ConditionalOnMissingClass
Using these conditions, Spring will only use the marked auto-configuration bean if the class in the annotation's argument is present/absent:
@Configuration @ConditionalOnClass(DataSource.class) class MySQLAutoconfiguration { //... }
@ConditionalOnBean and @ConditionalOnMissingBean
@Bean @ConditionalOnBean(name = "dataSource") LocalContainerEntityManagerFactoryBean entityManagerFactory() { // ... }
@ConditionalOnProperty
@Bean @ConditionalOnProperty( name = "usemysql", havingValue = "local" ) DataSource dataSource() { // ... }
@ConditionalOnResource
@ConditionalOnResource(resources = "classpath:mysql.properties") Properties additionalProperties() { // ... }
@ConditionalOnWebApplication and @ConditionalOnNotWebApplication
@ConditionalOnWebApplication HealthCheckController healthCheckController() { // ... }
@ConditionalExpression
@Bean @ConditionalOnExpression("${usemysql} && ${mysqlserver == 'local'}") DataSource dataSource() { // ... }
@Conditional
we can create a class evaluating the custom condition. We tell Spring to use this custom condition with @Conditional:
@Conditional(HibernateCondition.class) Properties additionalProperties() { //... }