The Spring Framework provides several powerful annotations that help in building clean, modular, and maintainable applications. Among them, the Stereotype Annotations play a crucial role in defining Spring-managed components. In this blog, we will explore what stereotype annotations are, their types, and when to use them with real-world examples and a visual diagram.
📌 What are Stereotype Annotations?
Stereotype annotations in Spring are special annotations that indicate a class
should be registered as a Spring bean. These annotations are part of the
org.springframework.stereotype
package. They also help categorize
Spring beans based on their roles (e.g., controller, service, repository).
🔹 Common Stereotype Annotations
Spring provides the following stereotype annotations:
@Component
@Service
@Repository
@Controller
1. @Component
This is the most generic annotation that indicates the class is a Spring component (bean).
import org.springframework.stereotype.Component;
@Component
public class MyUtility {
public void print() {
System.out.println("This is a utility class.");
}
}
2. @Service
Used to annotate service-layer classes. It's a specialization of
@Component
.
import org.springframework.stereotype.Service;
@Service
public class UserService {
public String getUser() {
return "John Doe";
}
}
3. @Repository
Used for DAO (Data Access Object) classes. It also enables exception translation into Spring's data access exceptions.
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository {
public void saveUser(String name) {
// Simulate saving user
}
}
4. @Controller
Used in web applications to define controller classes that handle HTTP requests.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HomeController {
@GetMapping("/")
@ResponseBody
public String home() {
return "Welcome to Spring!";
}
}
🧠Internal Structure of Stereotype Annotations
All these annotations internally use @Component
. That means they
are just specialized forms of @Component
with additional
semantics.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
String value() default "";
}
🖼️ Diagram: Spring Stereotype Annotation Hierarchy
Diagram: All stereotype annotations in Spring derive from @Component
⚙️ How Spring Registers These Beans
To enable Spring to scan and register classes with stereotype annotations, you
need to use @ComponentScan
:
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}
✅ When to Use What?
- @Component: For generic Spring-managed beans.
- @Service: For business logic and service classes.
- @Repository: For database interaction and DAO layers.
- @Controller: For web controllers that handle HTTP requests.
🧩 Conclusion
Stereotype annotations not only help Spring register your classes as beans but also bring clarity and structure to your code. They make your application more readable and maintainable. Always use the most specific annotation that fits your class's role to follow Spring’s design philosophy and best practices.
📎 References
Happy Spring-ing! 🌱
0 Comments