In this article, we will take a look at the list of annotations used in Spring Boot so that you can have a clear idea of which annotation does what.
List of Spring Boot Annotation
@Bean | @Service | @Repository |
@Controller | @RequestMapping | @Autowired |
@SpringBootApplication | @EnableAutoConfiguration | @Required |
@Qualifier | @ComponentScan | @Lazy |
@Configuration | @Component | @CookieValue |
Let us understand each one by one
1. @Bean
It is used at the Method level. It tells us that the method produces Bean which will be managed by Spring Container.
Example:
@Bean Public Example beanEg(){ return new Example(); }
2. @Service
It is used at the Class Level. It tells us that the class is a service class i.e It will contain all the business logic.
Example:
@Service public class ServiceClass{ public vois method{ //Business Logic } }
3. @Repository
It is used at the class level. It tells us that the annotated class is a Data Access layer.
Example:
@Repository public class ClassRepository{ public void add(){ //Persistence Code : save data to db } }
4. @Controller
It is used at the class level. It tells us that the class is a handler for the incoming request. It mostly returns web pages.
Example:
@Controller public class TestController{ }
5. @RequestMapping
It can be used with the class as well as on method. It is used to map the HTTP request. It has optional elements such as name, method, value, consumes, required, path and, etc.
Example:
@Controller public class TestController{ @RequestMapping("/test") public String home(Model model){ return "home" ; } }
6. @Configuration
It is used with class. It is used as source Bean definition.
Example:
@Configuration public class TestConfiguration{ @Bean ... ... ... }
7. @Autowired
It injects object dependency implicitly. It is used so that the spring container auto-wires the bean by its matching data type.
Example:
@Component public class Student private Address address; @Autowired public Student(Address address) { this.address=address } }
8. @Component
It is a class-level annotation. It makes a class into the bean and will be auto-detected.
Example:
@Component public class Student{ .... .... }
9. @ComponentScan:
It is used to scan packages for the beans. It is used along with @Configuration to allow Spring to know which package to scan.
Example:
@ComponentScan(basePackages = “com.abc”) @Configuration Public class ClassExample { .... .... }
10. @SpringBootApplication
It is a combination of @Configuration, @ComponentScan, @EnableAutoConfigration. It does the component scan.
Example:
@SpringBootApplication public class MyFirstSbProjectApplication { public static void main(String[] args) { SpringApplication.run(MyFirstSbProjectApplication.class, args); } }
11. @EnableAutoConfiguration
It is applied to the main application class. It enables the Spring Boot auto-configuration mechanism.
12. @Required
It is applied to bean property setter methods. It fails the configuration if dependency is not injected. If you want to see the example check this article Annotation Based Configuration in Spring Framework
13. @Qualifier
It is used when more restriction is required while injecting dependency. when more than one bean of the same type is created, and only one of them is to be wired with a property, @Qualifier is used.
14. @CookieValue
It is used at the method parameter level as an argument of the request mapping method.
15. @Lazy
It indicates whether a bean is to be lazily initialized.
Thus, these are all the annotations that we have used in Spring and will be using in Spring Boot on demand.
Now, as we know, Spring Boot is used for creating Rest APIs. so, we will see the list of some REST Annotations.
List of REST Annotation
1. @ResponseBody
It is used when we don’t want to return the view from the request handling method of the controller class. It tells the framework to return an object into JSON or XML format.
2. @RestController
It is the combination of @Controller & @ResponseBody. It basically eliminates the need to annotate every request handling method of the controller class with @ResponseBody annotation.
3. @GetMapping
It maps the specific HTTP GET Request on the handler method of the controller class. It is used to create web service endpoints that fetch data.
4. @PostMapping
It maps the specific HTTP POST Request on the handler method of the controller class. It is used to create web service endpoints that create data.
5. @PutMapping
It maps the specific HTTP PUT Request on the handler method of the controller class. It is used to create web service endpoints that create or updates data.
6. @DeleteMapping
It maps the specific HTTP DELETE Request on the handler method of the controller class. It is used to create web service endpoints that delete data.
7. @PatchMapping
It maps the specific HTTP PATCH Request on the handler method of the controller class.
Thus, these are all the annotations that we will use to create RESTful Web Services in Spring Boot.