Sobes.tech
Middle+

List the Spring annotations that you frequently use.

sobes.tech AI

Answer from AI

Frequently used Spring annotations:

  • @Component — the basic annotation for registering a component in the Spring context.
  • @Service — a specialized annotation for the service layer.
  • @Repository — for the DAO layer, with support for database exception handling.
  • @Controller and @RestController — for defining controllers in MVC and REST API respectively.
  • @Autowired — for automatic dependency injection.
  • @Configuration — for Spring configuration classes.
  • @Bean — for defining beans in configuration classes.
  • @RequestMapping, @GetMapping, @PostMapping — for routing HTTP requests.
  • @Transactional — for transaction management.

Example:

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.findById(id);
    }
}
List the Spring annotations that you frequently use… - sobes.tech