Sobes.tech
Junior — Middle

Can you list the main annotations used in Spring Framework?

sobes.tech AI

Answer from AI

The following main annotations are often used in the Spring Framework:

  • @Component — marks a class as a Spring component for automatic detection and management.
  • @Service — a specialized annotation for the service layer, semantically indicating business logic.
  • @Repository — for the data access layer, also handles database exceptions.
  • @Controller — to designate controllers in MVC.
  • @RestController — a combination of @Controller and @ResponseBody, used for REST APIs.
  • @Autowired — for automatic dependency injection.
  • @Qualifier — specifies which bean to inject when there are multiple.
  • @Value — for injecting values from configuration.
  • @Configuration — a class with Spring configuration.
  • @Bean — a method that creates a bean in a configuration class.
  • @RequestMapping and its derivatives (@GetMapping, @PostMapping, etc.) — for routing HTTP requests.

Example usage:

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public User findUser(Long id) {
        return userRepository.findById(id).orElse(null);
    }
}