Spring Boot Annotations

In this quick article, we’ll explore the annotations from the org.springframework.boot.autoconfigure and org.springframework.boot.autoconfigure.condition packages.

This article is part of a series:
As we know that Spring Boot is a brand new framework from the team at Pivotal, designed to simplify the bootstrapping and development of a new Spring application. The framework takes an opinionated approach to configuration, freeing developers from the need to define a boilerplate configuration. It provides defaults for code and annotation configuration to quick start new Spring projects within no time.
Spring boot provides many annotations for autoconfiguration from the org.springframework.boot.autoconfigure and org.springframework.boot.autoconfigure.condition packages. Let's list all the annotations from these packages.

1. @SpringBootApplication

@SpringBootApplication annotation indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.
@SpringBootApplication annotation example: We use this annotation to mark the main class of a Spring Boot application:
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);
    }
}

2. @EnableAutoConfiguration

@EnableAutoConfiguration annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web dependency added to classpath leads to configure Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.
@EnableAutoConfiguration annotation example: Let's add @EnableAutoConfiguration annotation to Application class or Main class to enable an auto-configuration feature.
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 @SpringBootApplication 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.
Example: In below example, using these conditions, Spring will only use the marked auto-configuration bean if the class in the annotation’s argument is present/absent:
@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: Use when we want to define conditions based on the presence or absence of a specific bean:
@Bean
@ConditionalOnBean(name = "dataSource")
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    // ...
}
@ConditionalOnMissingBean annotation example: When placed on a @Bean method, the target type defaults to the return type of the method, as shown in the following example:
@Configuration
public class MyAutoConfiguration {

 @Bean
 @ConditionalOnMissingBean
 public MyService myService() { ... }
}
In the preceding example, the myService bean is going to be created if no bean of type MyService is already contained in the ApplicationContext.

5. @ConditionalOnProperty

The @ConditionalOnProperty annotation lets configuration be included based on a Spring Environment property.
@ConditionalOnProperty annotation example: With this annotation, we can make conditions on the values of properties:
@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”. A web application is an application that uses a Spring WebApplicationContext, defines a session scope, or has a StandardServletEnvironment.
@ConditionalOnWebApplication annotation sample code: With these annotations, we can create conditions based on if the current application is or isn’t a web application:
@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 true:
@Bean
@ConditionalOnExpression("${usemysql} && ${mysqlserver == 'local'}")
DataSource dataSource() {
    // ...
}

9. @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:
@Conditional(HibernateCondition.class)
Properties additionalProperties() {
    //...
}
Learn complete Spring Boot on Spring Boot Tutorial

Comments