Spring Boot Specific
About
Spring Boot provides several annotations that simplify configuration, auto-configuration, and conditional bean loading.
1. Core Spring Boot Annotations
These annotations help in configuring and bootstrapping a Spring Boot application.
@SpringBootApplication
@SpringBootApplicationThis is the main entry point for a Spring Boot application.
It is a combination of:
@Configuration– Marks the class as a Spring configuration class.@EnableAutoConfiguration– Enables automatic configuration based on dependencies.@ComponentScan– Scans for components in the same package and sub-packages.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}@EnableAutoConfiguration
@EnableAutoConfigurationEnables Spring Boot’s auto-configuration feature, which automatically configures beans based on the classpath.
It is included in
@SpringBootApplication.
@Configuration
@EnableAutoConfiguration
public class MyConfig {
}2. Conditional Annotations
These annotations conditionally enable or disable beans based on various criteria.
@ConditionalOnProperty
@ConditionalOnPropertyLoads a bean only if a specified property exists in
application.propertiesorapplication.yml.
// If feature.enabled=true, MyFeature bean is created.
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
@Bean
public MyFeature myFeature() {
return new MyFeature();
}@ConditionalOnClass
@ConditionalOnClassLoads a bean only if a specified class is present in the classpath.
// If ExternalLibrary is available, myService() is registered.
@ConditionalOnClass(name = "com.example.ExternalLibrary")
@Bean
public MyService myService() {
return new MyService();
}@ConditionalOnMissingBean
@ConditionalOnMissingBeanRegisters a bean only if no other bean of the same type exists.
// If MyService is already defined elsewhere, this bean won’t be created.
@Bean
@ConditionalOnMissingBean(MyService.class)
public MyService defaultService() {
return new MyService();
}@ConditionalOnBean
@ConditionalOnBeanRegisters a bean only if another specified bean exists.
// MyService is created only if MyDependency is present.
@Bean
@ConditionalOnBean(MyDependency.class)
public MyService myService() {
return new MyService();
}Last updated