InnovationM Blog

Spring Boot Annotations

Spring Boot is known for its simplicity and ease of use, largely due to its extensive use of annotations. These annotations help configure and manage the application in a declarative way, reducing boilerplate code and enhancing readability. 

Here, we’ll explore some of the most commonly used annotations in Spring Boot.

1. @SpringBootApplication

This is a convenience annotation that combines three annotations:

  • @Configuration: Indicates that the class can be used by the Spring IoC container as a source of bean definitions.
  • @EnableAutoConfiguration: Enables Spring Boot’s auto-configuration mechanism.
  • @ComponentScan: Tells Spring to scan the current package and its sub-packages for components, configurations, and services.
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}

2. @RestController

Combines @Controller and @ResponseBody. It marks the class as a web controller, capable of handling HTTP requests and returning the response body directly.

@RestController
public class MyController {
@GetMapping("/hello")
public String sayHello() { return "Hello, World!";
}
}

3. @RequestMapping

Used to map web requests to specific handler methods or classes. It can be used at the class level and method level.

@RestController
@RequestMapping("/api")
public class ApiController {
@GetMapping("/users")
public List<User>
}
getUsers() {
// Implementation here
}

4. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping

These annotations are shortcuts for @RequestMapping with specific HTTP methods.

  • GetMapping      –  retrieve the data from the database.

  • PostMapping    –  maps specific URLs to handler methods allowing you to receive and process data submitted through POST requests.

  • PutMapping      – modify/update a resource where the client sends data that updates the entire resource. It is used to set an entity’s information completely. PUT is similar to POST in that it can create resources, but it does so when there is a defined URI. PUT overwrites the entire entity if it already exists, and creates a new resource if it doesn’t exist.

  • DeleteMapping  – maps a specific URLs handler method allowing you to receive and process the data submitted through DELETE requests.

@RestController @RequestMapping("/api")
public class ApiController {
@GetMapping("/users")
public List<User> getUsers() {
}
// Implementation here
@PostMapping("/users")
public User createUser (@RequestBody User user) {
// Implementation here
}
}

5. @Autowired

Used for automatic dependency injection. Spring’s dependency injection allows you to manage dependencies in a declarative way.

@Service
public class UserService {
}
@Autowired
private UserRepository userRepository;
public List<User> getAllUsers() { return userRepository.findAll();
}

6. @Component, @Service, @Repository, @Controller

These annotations are used to define Spring-managed components. They make the classes eligible for Spring’s component scanning to detect and register as beans.

= @Component: Generic stereotype for any Spring-managed component.

= @Service: Specialization of @Component for service-layer beans.

= @Repository: Specialization of @Component for data access objects (DAOs).

= @Controller: Specialization of @Component for presentation-layer beans.

@Service
public class MyService {
}
// Service logic here
@Repository
public class MyRepository { // Repository logic here
}

7. @Value

Used to inject values into fields from properties files.

@Component
public class MyComponent {
}
@Value("${my.property}")
private String myProperty;

Leave a Reply