Tag Archives: Spring Bean Annotations

Spring Boot Annotations List


Here you will know about  all Spring Boot related annotations which we mostly used while development of applications:

See Also :

Spring Boot Annotations

Annotations & Description
@SpringBootAnnotation : This annotation is use to mark the main class of Spring boot application. It encapsulate below transactions with it’s default value.

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan

Example

@SpringBootApplication
class CarFactoryApplication {

    public static void main(String[] args) {
        SpringApplication.run(CarFactoryApplication.class, args);
    }
}
@EnableAutoConfiguration : Spring boot looks for auto-configuration beans on its classpath and automatically applies them.
Note: Always use this annotaion with @Configuration.Usually, when we write auto-configuration, use them conditionally with @Configuration classes or @Bean methods. as given in below annotations:
Example

@Configuration
@EnableAutoConfiguration
class CarFactoryConfig {}
@ConditionalOnClass and @ConditionalOnMissingClass
These annotations use when need to marked auto-configuration bean if the class in the annotation’s argument is present/absent.
Example

@Configuration
@ConditionalOnClass(DataSource.class)
class DBConfiguration {
    //…
}
@ConditionalOnBean and @ConditionalOnMissingBean
These annotations use when need to define conditions based on the presence or absence of a specific bean.
Example

@Bean
@ConditionalOnBean(name = "dataSource")
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    // …
}
@ConditionalOnProperty :
This annotation is use when need to apply condition based on properties values.
Example

@Bean
@ConditionalOnProperty(
    name = "useoracle",
    havingValue = "local"
)
DataSource dataSource() {
    // ...
}
@ConditionalOnResource :
This annotation will use a definition only when a specific resource is present.
Example

@ConditionalOnResource(resources = "classpath:database.properties")
Properties additionalProperties() {
    // ...
}
@ConditionalOnWebApplication and @ConditionalOnNotWebApplication :
Use these annotations, when condition need to check if the current application is or isn’t a web application.
Example

@ConditionalOnWebApplication
HealthCheckController healthCheckController() {
    // ...
}
@ConditionalExpression :
This annotation use in more complex situations and marked definition when the SpEL expression is evaluated to true.
Example

@Bean
@ConditionalOnExpression("${useoracle} && ${oracleserver == 'local'}")
DataSource dataSource() {
    // ...
}
@Conditional :
For more complex conditions, we can create a class for evaluating the custom condition.
Example

@Conditional(HibernateCondition.class)
Properties additionalProperties() {
    //...
}