Spring Boot Annotations with Examples

Jiledar_Dhakad
3 min readFeb 21, 2023

In this topic, we will learn about Spring Boot Annotations. Spring Boot Annotations are available in the org.springframework.boot.autoconfigure and org.springframework.boot.autoconfigure.condition packages. These are some of the following Spring Boot annotations:
• @SpringBootApplication
• @SpringBootConfiguration
• @EnableAutoConfiguration
• @ConditionalOnClass, and @ConditionalOnMissingClass
• @ConditionalOnBean, and @ConditionalOnMissingBean
• @ConditionalOnProperty
• @ConditionalOnResource
• @ConditionalOnWebApplication and @ConditionalOnNotWebApplication
• @ConditionalExpression
• @Conditional

@SpringBootApplication:

This @SpringBootApplication annotation is used to mark the main class of the Spring Boot project. This annotation binds @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes.

Example:

@SpringBootApplication
public class TestApplication {
public static void main(String[] args){
SpringApplication.run(TestApplication.class, args);
}
}

@SpringBootConfiguration Annotation:

This @SpringBootConfiguration annotation is a class-level annotation that is part of Spring Boot. This annotation implies that a class provides Spring Boot project configuration. This annotation can be used as an alternative to Spring framework's standard @Configuration annotation so that configuration can be found automatically. Most Spring Boot Applications use @SpringBootConfiguration through @SpringBootApplication. If an application uses @SpringBootApplication annotation, it already uses @SpringBootConfiguration annotation.

Example:

@SpringBootConfiguration
public class Test {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
}

@EnableAutoConfiguration Annotation:

This @EnableAutoConfiguration annotation auto-configures the beans that are present in the classpath of the project. This annotation simplifies the developer’s work by assuming the required beans from the classpath and configuring it to run the application. This annotation is part of Spring Boot. For example, spring-boot-starter-web dependency in the classpath, Spring boot auto-configures Tomcat, and Spring MVC in the Spring Boot Project. A class package declaring this @EnableAutoConfiguration annotation is considered the default of the Spring Boot project. Thus, we need to apply the @EnableAutoConfiguration annotation in the root package to examine every sub-package and class.

Example:

@Configuration
@EnableAutoConfiguration
public class Test {
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
}

@ConditionalOnClass Annotation and @ConditionalOnMissingClass Annotation:

This @ConditionalOnClass annotation is used to mark the auto-configuration bean if the class in the annotation’s argument is present/absent in the Spring Boot Project.

Example:

@Configuration
@ConditionalOnClass(MongoDBService.class)
class MongoDBConfiguration {
-----------------------
-----------------------
}

@ConditionalOnBean Annotation and @ConditionalOnMissingBean Annotation:

These two annotations of the Spring Boot are used to let a bean be included based on the presence or absence of specific beans in the Spring Boot project.

Example:

@Bean
@ConditionalOnMissingBean(type = "JpaTransactionManager")
JpaTransactionManager jpaTransactionManager(EntityManagerFactory entityManagerFactory){
------------------------
------------------------
}

@ConditionalOnProperty Annotation:

This @ConditionalOnProperty Spring Boot annotation is used to let configuration be included based on the presence and value of a Spring Environment property in the project.

Example:

@Bean
@ConditionalOnProperty(name = "usemongodb",havingValue = "local")
DataSource dataSource(){
-------------------
-------------------
}
@Bean
@ConditionalOnProperty(name = "usemongodb",havingValue = "prod")
DataSource dataSource(){
--------------------
--------------------
}

@ConditionalOnResource Annotation:

This @ConditionalOnResource annotation is used to let configuration be included only when a specific resource is present in the classpath of the project.

Example:

@ConditionalOnResource(resources="classpath:mongodb.properties")
Properties additionalProperties(){
-----------------------
-----------------------
}

@ConditionalOnExpression Annotation:

This @ConditionalOnExpression annotation is used to let configuration be included based on the result of the SpEL (Spring Expression Language) expression. SpEL (Spring Expression Language): This is an expression language that supports querying and manipulating an object graph at runtime.

Example:

@Bean
@ConditionalOnExpression("${env} && ${havingValue == 'local'}")
DataSource dataSource(){
------------------
------------------
}

@ConditionalOnCloudPlatform Annotation:

This @ConditionalOnCloudPlatform annotation is used to let configuration be included when the specified cloud platform is active in the Spring Boot project.

Example:

@Configuration
@ConditionalOnCloudPlatform(CloudPlatform.CLOUD_FOUNDRY)
public class CloudConfigurationExample{
-------------------
-------------------
}

Conclusion:

In this topic, we learnt about various annotations of Spring Boot.

--

--

Jiledar_Dhakad

Spring Java tutorials provides well contents to learn java, spring framework and also the architecture of spring technology for beginners and professionals.