Labs ICT
โญ Pro Login

Spring Annotations

The magic behind Spring's configuration.

Spring Annotations

Annotations in Spring Boot are like sticky notes that tell Spring what to do with your classes. They're simple markers that add metadata to your code. Instead of writing lots of XML configuration, you just sprinkle these annotations around and Spring does the heavy lifting.

The most common stereotype annotations are @Component, @Service, @Repository, and @Controller. They all tell Spring to manage the class as a bean, but they have different semantic meanings. Using the right one makes your code more readable and maintainable.

Think of them as different job titles in a company. @Controller handles HTTP requests, @Service contains business logic, @Repository deals with data access, and @Component is the general-purpose fallback when the other stereotypes don't fit.

@Autowired and Dependency Injection

@Autowired is the annotation that enables dependency injection. When you put it on a constructor, setter, or field, Spring automatically injects the required bean. It's like having a personal shopper who knows exactly what you need.

Remember our earlier discussion about constructor injection? That's where @Autowired shines. You annotate the constructor and Spring passes in all the dependencies. It's clean, explicit, and testable.

@Service
public class UserService {
    
    private final UserRepository userRepository;
    private final EmailService emailService;
    
    @Autowired
    public UserService(UserRepository userRepository, EmailService emailService) {
        this.userRepository = userRepository;
        this.emailService = emailService;
    }
    
    public User registerUser(String email) {
        User user = new User(email);
        userRepository.save(user);
        emailService.sendWelcomeEmail(user);
        return user;
    }
}
Try it Yourself โ†’

@Configuration and @Bean

@Configuration is used on classes that define beans. It's like a factory blueprint. Inside a @Configuration class, you can use @Bean methods to create specific bean instances. This is useful when you need to configure third-party libraries or have complex bean creation logic.

The @Bean annotation goes on methods within @Configuration classes. Each method returns an instance that Spring will manage. It's perfect for situations where you can't simply annotate a class with @Component.

When to use which? Use @Service for business logic, @Repository for data access, @Controller for web controllers, and @Component when none of the others fit. @Configuration is for when you need explicit control over bean creation. Simple as that.

Other Useful Annotations

Beyond the core stereotypes, Spring Boot has annotations for web development. @RestController combines @Controller and @ResponseBody, making it easy to create REST APIs. @RequestMapping maps HTTP requests to handler methods.

For dependency injection, @Autowired is the most common, but you can also use @Inject from the JSR-330 standard. Both work similarly, though @Autowired has some additional features like required attribute.

The key is to start with the basic annotations and gradually learn more as needed. Spring Boot's annotation system is extensive, but you don't need to learn everything at once. Master the fundamentals first, then explore more specialized annotations as your projects require them.

๐Ÿงช Quick Quiz

What is @Service used for?