Jānis Ošs’ Post

🚀 Spring Boot Daily Learning Are you using @Component on every class and wondering why Spring gives you 4 different stereotype annotations? Here's what actually separates them — and why it matters in production. Spring's stereotype annotations all register beans in the IoC container, but they carry different semantic weight and unlock different framework features: @Component // Generic bean — use as last resort @Service // Business logic layer @Repository // Data access layer + exception translation @Controller // MVC presentation layer (with @RestController for REST) The critical difference? @Repository activates Spring's PersistenceExceptionTranslationPostProcessor — it automatically translates low-level JPA/Hibernate exceptions into Spring's unified DataAccessException hierarchy. Your service layer never leaks Hibernate internals. @Service and @Controller signal architectural intent. Spring AOP, Spring Security, and your teammates all rely on these contracts to apply cross-cutting concerns correctly. Using @Component everywhere breaks that contract. Best practice: Always pick the most specific annotation. @Component is for custom infrastructure beans — not business or data code. #Java #SpringBoot #BackendDevelopment #SpringFramework #CleanCode

To view or add a comment, sign in

Explore content categories