- org.springframework.boot.autoconfigure
- org.springframework.boot.autoconfigure.condition
This article is part of a series:
1. @SpringBootApplication
@SpringBootApplication Annotation Example
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }Basically, the @SpringBootApplication annotation is a combination of the following three Spring annotations:

@Configuration
This annotation marks a class as a Configuration class for Java-based configuration.@ComponentScan
This annotation enables component-scanning so that the web controller classes and other components you create will be automatically discovered and registered as beans in Spring's Application Context.@EnableAutoConfiguration
This annotation enables the magical auto-configuration feature of Spring Boot, which can automatically configure a lot of stuff for you.Read more about @SpringBootApplication annotation on Spring Boot @SpringBootApplication Annotation with Example.
2. @EnableAutoConfiguration
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>@EnableAutoConfiguration Annotation Example
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Read more about @EnableAutoConfiguration annotation on Spring Boot @EnableAutoConfiguration Annotation with Example
3. @ConditionalOnClass and @ConditionalOnMissingClass
These annotations belong to Class conditions. The @ConditionalOnClass and @ConditionalOnMissingClass annotations let configuration be included based on the presence or absence of specific classes.@Configuration
@ConditionalOnClass(DataSource.class)
class OracleAutoconfiguration {
//...
}
4. @ConditionalOnBean and @ConditionalOnMissingBean
The @ConditionalOnBean and @ConditionalOnMissingBean annotations let a bean be included based on the presence or absence of specific beans.@ConditionalOnBean Annotation Example
@Bean
@ConditionalOnBean(name = "dataSource")
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
// ...
}
@ConditionalOnMissingBean Annotation Example
@Configuration
public class MyAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public MyService myService() { ... }
}
5. @ConditionalOnProperty
The @ConditionalOnProperty annotation lets configuration be included based on a Spring Environment property.@ConditionalOnProperty Annotation Example
@Bean
@ConditionalOnProperty(
name = "usemysql",
havingValue = "local"
)
DataSource dataSource() {
// ...
}
6. @ConditionalOnResource
The @ConditionalOnResource annotation lets configuration be included only when a specific resource is present:@ConditionalOnResource(resources = "classpath:mysql.properties")
Properties additionalProperties() {
// ...
}
7. @ConditionalOnWebApplication and @ConditionalOnNotWebApplication
The @ConditionalOnWebApplication and @ConditionalOnNotWebApplication annotations let configuration be included depending on whether the application is a “web application”.@ConditionalOnWebApplication annotation sample code
@ConditionalOnWebApplication
HealthCheckController healthCheckController() {
// ...
}
8. @ConditionalExpression
We can use this annotation in more complex situations. Spring will use the marked definition when the SpEL expression is evaluated to be true:@Bean
@ConditionalOnExpression("${usemysql} && ${mysqlserver == 'local'}")
DataSource dataSource() {
// ...
}
9. @Conditional
For even more complex conditions, we can create a class evaluating the custom condition.@Conditional(HibernateCondition.class)
Properties additionalProperties() {
//...
}
Learn complete Spring Boot on Spring Boot Tutorial
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment