Spring Boot Annotations

@SpringBootApplication

We use this annotation to mark the main class of a Spring Boot application:
1
2
3
4
5
6
7
@SpringBootApplication
class VehicleFactoryApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(VehicleFactoryApplication.class, args);
    }
}
@SpringBootApplication encapsulates @Configuration@EnableAutoConfiguration, and @ComponentScanannotations with their default attributes.

@EnableAutoConfiguration

@EnableAutoConfiguration, as its name says, enables auto-configuration. It means that Spring Boot looks for auto-configuration beans on its classpath and automatically applies them.
Note, that we have to use this annotation with @Configuration:
1
2
3
@Configuration
@EnableAutoConfiguration
class VehicleFactoryConfig {}

Auto-Configuration Conditions

Usually, when we write our custom auto-configurations, we want Spring to use them conditionally. We can achieve this with the annotations in this section.
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:
1
2
3
4
5
@Configuration
@ConditionalOnClass(DataSource.class)
class MySQLAutoconfiguration {
    //...
}

@ConditionalOnBean and @ConditionalOnMissingBean

We can use these annotations when we want to define conditions based on the presence or absence of a specific bean:
1
2
3
4
5
@Bean
@ConditionalOnBean(name = "dataSource")
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    // ...
}

@ConditionalOnProperty

With this annotation, we can make conditions on the values of properties:
1
2
3
4
5
6
7
8
@Bean
@ConditionalOnProperty(
    name = "usemysql",
    havingValue = "local"
)
DataSource dataSource() {
    // ...
}

@ConditionalOnResource

We can make Spring to use a definition only when a specific resource is present:
1
2
3
4
@ConditionalOnResource(resources = "classpath:mysql.properties")
Properties additionalProperties() {
    // ...
}

@ConditionalOnWebApplication and @ConditionalOnNotWebApplication

With these annotations, we can create conditions based on if the current application is or isn’t a web application:
1
2
3
4
@ConditionalOnWebApplication
HealthCheckController healthCheckController() {
    // ...
}

@ConditionalExpression

We can use this annotation in more complex situations. Spring will use the marked definition when the SpEL expression is evaluated to true:
1
2
3
4
5
@Bean
@ConditionalOnExpression("${usemysql} && ${mysqlserver == 'local'}")
DataSource dataSource() {
    // ...
}

@Conditional

For even more complex conditions, we can create a class evaluating the custom condition. We tell Spring to use this custom condition with @Conditional:
1
2
3
4
@Conditional(HibernateCondition.class)
Properties additionalProperties() {
    //...
}

Comments

Popular posts from this blog

gsutil Vs Storage Transfer Service Vs Transfer Appliance

SQL basic interview question