
Spring Boot annotations are meant to simplify configuration and eliminate XML configurations which are complex as compared to annotations.
When we read Spring, we see that the configuration that took place there can be done in a variety of ways. The configuration in the previous Spring version was based on XML. To configure the system, we used to work in the XML file. We used to make an xml file, and we had to define beans there. The style of configuration changed when new versions were released, such as Java-based configuration and annotation-based configuration come there.
We don’t have to put much work into annotation-based configuration, and we don’t have to create a separate xml file.
Annotations may be used to configure the spring framework in its newest version. As a result, it is recommended that you use Java or Annotation-based setup.
The majority of the annotations come from the Spring Framework. However, the example we’ll provide will be used in Spring Boot. These annotations are applicable to both.
Table of Contents
List of Spring Boot Annotations
There are several spring boot annotations available, which are listed below.
@SpringBootApplication
@SpringBootApplication is responsible for auto configuration. If we use this annotation the components are scanned and configured.
@SpringBootApplication is equivalent to the @Configuration, @ComponentScan and @EnableAutoConfiguration annotations. @SpringBootApplication is used at the class level and this annotation is placed in the base package.
The main class of the Spring Boot application is the Java class annotated with @SpringBootApplication, and it is from here that the application starts.
package com.geekcer.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// Same as @Configuration @EnableAutoConfiguration @ComponentScan
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Bean
@Bean annotation is method level annotation. If we use this annotation then method should returns a bean that the Spring container manages that bean.
public class BeanExample { @Bean public Employee getEmployee() { return new Employee(); } }
@Component
Whenever we use @Component annotation, we are specifying that this component will be managed by Spring. Spring will be in control of this class object. Because it is a component of a spring.
This is an annotation at the class level. It’s used to indicate that a class is a bean. When we annotate a class with @component, it becomes available in the class path, where it is picked up by the spring framework, which configures the application environment and starts utilizing it as a spring bean.
@Component public class Employee { ..... }
@Autowired
The Spring bean is autowired using the @Autowired annotation. It can be used with constructors, setter methods and instance variables.
It implicitly injects object dependencies. The Spring container auto-wires the bean with its matching data type when we use this annotation.
public class Emplyee { @Autowired private Department dept; private String empName; }
@EnableAutoConfiguration
Auto configuration is enabled when @EnableAutoConfiguration is specified. Spring Boot will now start inserting beans as a result of this annotation. These instructions are based on the class path, other beans and other property settings.
@EnableAutoConfiguration annotation is used in the application’s main class.
@Configuration @EnableAutoConfiguration public class GeekApplicationConfig { ... }
How to disable a specific auto-configuration class? To exclude/disable a certain class, we may utilize the @EnableAutoConfiguration annotation’s exclude property.
@Configuration // Use of exclude @EnableAutoConfiguration(exclude ={classname}) public class GeekApplicationConfig { ... }
@ComponentScan
The @ComponentScan annotation is used to scan a package. It’s used in combination with @configuration. We can also specify the package in @ComponentScan.
@Configuration @ComponentScan(basePackages = "com.geekcer") public class Department { ..... }
@Qualifier
When using the @Bean annotation and creating many beans of the same type, there may be some confusion when autowiring which bean to use.
The @Qualifier annotation is used to eliminate this confusion. We give each bean a tag/name using the @Qualifier, and we also use @Autowire with that tag/name.
Click here to to learn the scopes of the bean.