What Is Spring Boot? How and Where to use Spring Boot Annotations?

@Bean, @Component, @Service, @Controller and @RestController

Prashanth Billa
4 min readNov 26, 2021

Spring Boot is an open-source Java-based framework used to create a micro Service. It is developed by the Pivotal Team and is used to build stand-alone and production-ready spring applications. This chapter will give you an introduction to Spring Boot and familiarizes you with its basic annotations.

What is Spring Boot?

Spring Boot provides a good platform for Java developers to develop a stand-alone and production-grade spring application that you can just run. You can get started with minimum configurations without the need for an entire Spring configuration setup.

The entry point of the spring boot application is the class contains @SpringBootApplication annotation and the main method.

Advantages

Spring Boot offers the following advantages to its developers

  • Easy to understand and develop spring applications
  • Increases productivity
  • Reduces the development time

Spring-boot Annotations

1.@Bean

Spring @Bean annotation tells that a method produces a bean to be managed by the Spring container. It is a method-level annotation. During Java configuration (@Configuration ), the method is executed and its return value is registered as a bean within a Bean Factory .


class MainApplication{
@Bean
public User getUser() {
return new User();
}
}
class User{
}
  • Inside of our class annotated with @Configuration, we can create a bean generating method
  • User might represent a local class, or it might be an external class. It doesn’t matter because we simply need to return an instance of it.

2.@Component

@Component is an annotation that allows Spring to automatically detect our custom beans. In other words, without having to write any explicit code, Spring will scan our application for classes annotated with @Component and Instantiate them and inject any specified dependencies into them.

Spring-boot provides annotation-based auto-wiring by providing @Autowired annotation. It is used to auto wire spring bean on setter methods, instance variable, and constructor. When we use @Autowired annotation, the spring container auto-wires the bean by matching data-type.

@Component 
public class ComponentExample implements IComponentExample {
}
public interface IComponentExample{
}
@SpringBootApplication
public class MainApplication{
@Autowired
private IComponentExample iComponentExample;
}

3.@Service

Service Components are the class file which contains @Service annotation. These class files are used to write business logic in a different layer, separated from controller class file. The logic for creating a service component class file is shown here

public interface ProductService {
}
@Service
public class ProductServiceImpl implements ProductService {
}

This annotation is same like @Component annotation. But we mark beans with @Service to indicate that they’re holding the business logic

4.@Controller

The @Controller annotation is a specialization of the generic stereotype @Component annotation, which allows a class to be recognized as a Spring-managed component.

The @Controller annotation extends the use-case of @Component and marks the annotated class as a business or presentation layer. When a request is made, this will inform the DispatcherServlet to include the controller class in scanning for methods mapped by the @RequestMapping annotation.

@Controller 
@ResponseBody
@RequestMapping("/app")
public class ControllerExample {
@Autowired
private IRepo iRepo;

@GetMapping("/getAll")
public List<User> getAll() {
return "home";
}
}

We’ve applied a @ResponseBody annotation to the class-level of this controller. When the request handlers return data back and the response will be serialized to JSON before being returned to the client.

5.@RestController

The @RestController annotation in Spring is essentially just a combination of @Controller and @ResponseBody.

Every request handling method of the RestController class automatically serializes return objects into HttpResponse.

Spring RestController takes care of mapping request data to the defined request handler method. Once response body is generated from the handler method, it converts it to JSON or XML response.

@RestController
@RequestMapping("/app")
public class RestControllerExample {
@Autowired
private IRepo iRepo;

@GetMapping("/getAll")
public List<User> getAll() {
return irepo.getAll();
}

Here’s a quick overview of these annotations:

  • @Bean annotation tells that a method produces a bean to be managed by the Spring container. It is a method-level annotation.
  • @Component is a generic stereotype for any Spring-managed component. It is a class level annotation.
  • @Service annotates classes at the service layer, which is used to write some business logic
  • @Controller annotates classes are the presentation and view layer (Spring-MVC), It will act as a controller, which controls requests and responses. We annotated the request handling method with @ResponseBody. This annotation enables automatic serialization of the return object into the HTTP Response.
  • @RestController is a specialized version of the controller. It includes the @Controller and @ResponseBody annotations, and as a result, simplifies the controller implementation. Every request handling method of the controller class automatically serializes return objects into HTTP Response.

--

--