Spring Boot Annotations

Spring boot has made configuration of spring and it’s dependencies really very easy with autoconfigure feature, most of the annotations are placed in the packages below.

org.springframework.boot.autoconfigure &
org.springframework.boot.autoconfigure.condition

Below you will see a few of the most used spring boot annotations :

1. @SpringBootApplication


This annotation is basically used on the class with main method, to mark the class as the main class of the application.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

The @SpringBootApplication annotation encapsulates @EnableAutoConfiguration, @Configuration and @ComponentScan annotations. Which enables their respective functionalities as well.

2. @SpringBootApplication


This annotation enables auto-configuration of the Spring Application Context, with the help of this annotation, spring attempts to look for beans that we are likely to add based on the presence of predefined classes in classpath.

For example, if we have embedded tomcat jar on the classpath, we are likely to want a TomcatServletWebServerFactory.

@Configuration
@EnableAutoConfiguration
@Import({ MyConfig1.class, MyConfig2.class })
public class EmployeeApplication {

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

}     
       

3. @Configuration


It indicates that a class provides Spring Boot application configuration.

4. @ComponentScan


Explicitly used with spring, but default in spring-boot. It scans all the Beans inside the package mentioned.

The @ComponentScan annotation is used with the @Configuration annotation to tell Spring the packages to scan for annotated components. @ComponentScan also used to specify base packages and base package classes using thebasePackageClasses or basePackages attributes of @ComponentScan.

See below an example

package com.programmertoday.base;
import com.programmertoday.SampleBean;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
  
@Configuration
@ComponentScan(basePackages = {
        "com.programmertoday.samplepackage1",
        "com.programmertoday.samplepackage2",
        "com.programmertoday.samplepackage3"
    },
    basePackageClasses = SampleBean.class)
public class EmployeeApplication {
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.
        run(EmployeeApplication.class, args);
        System.out.println("Contains SampleBean  : " + context.containsBeanDefinition("SampleBean"));
      }
}

package com.programmertoday.samplepackage1;
import org.springframework.stereotype.Component;
@Component("SampleBean")
public class SampleBean{
}

OUTPUT
Contains SampleBean  : true 
       

5. @AutoConfigureBefore, @AutoConfigureAfter, @AutoConfigureOrder


We can use the @AutoConfigureAfter or @AutoConfigureBefore annotations if our configuration needs to be applied in a specific order (like before after any other configuration). Write the annotation above the current Bean where you want to apply this order of application of configuration.

@Configuration
@AutoConfigureAfter(Cache1.class)
@ConditionalOnBean(CacheManager.class)
@ConditionalOnClass(CacheStatisticsProvider.class)
public class Cache2
{
    @Bean
    public SampleBean returnNewBean(){
        return new SampleBean ();
    }
}             
       

6. Condition annotations


It allows to register a bean only when the condition meets. When we write our custom auto-configurations, we want Spring to use them conditionally. We can achieve this with conditional annotations.

  1. @ConditionalOnBean and @ConditionalOnMissingBean : former means if bean is present , the latter means the if the bean is not present.
  2. @ConditionalOnClass and @ConditionalOnMissingClass : These annotations let configuration classes be included based on the presence or absence of specific classes.
  3. @ConditionalOnNotWebApplication and @ConditionalOnWebApplication : These annotations let configuration be included depending on whether the application is a “web application” or not
  4. @ConditionalOnProperty : This annotation lets configuration be included based on the presence and value of a Spring Environment property
  5. @ConditionalOnResource : This annotation lets configuration be included only when a specific resource is present in the classpath.
  6. @ConditionalOnExpression : This annotation lets configuration be included based on the result of a SpEL expression.
  7. @ConditionalOnCloudPlatform : This annotation lets configuration be included when the specified cloud platform is active.

Summary

In this spring boot annotations tutorial you have learnt about annotations and auto configuration features of spring boot. These are few of the most in use annotations which you might surely use in your spring boot application. Hope you liked it !