0
0
Spring Bootframework~8 mins

@Component, @Service, @Repository, @Controller in Spring Boot - Performance & Optimization

Choose your learning style9 modes available
Performance: @Component, @Service, @Repository, @Controller
MEDIUM IMPACT
These annotations affect application startup time and memory usage by controlling bean creation and scanning.
Defining beans for different application layers
Spring Boot
@Service
public class UserService { ... }

@Repository
public class UserRepository { ... }

@Controller
public class UserController { ... }
Using specific stereotypes helps Spring optimize scanning and apply layer-specific behaviors, reducing overhead.
📈 Performance GainReduces scanning overhead and memory usage; improves startup time
Defining beans for different application layers
Spring Boot
@Component
public class UserService { ... }

@Component
public class UserRepository { ... }

@Component
public class UserController { ... }
Using @Component everywhere causes Spring to scan and register all classes as generic beans, increasing startup time and memory usage.
📉 Performance CostIncreases startup time by scanning more classes; adds unnecessary beans to context
Performance Comparison
PatternBean ScanningStartup TimeMemory UsageVerdict
@Component everywhereHigh - scans all classes as generic beansSlower due to extra scanningHigher due to many beans[X] Bad
@Service, @Repository, @Controller usedOptimized - scans with layer contextFaster startupLower memory footprint[OK] Good
Rendering Pipeline
Spring scans classes with these annotations during startup to create and register beans in the application context.
Classpath Scanning
Bean Creation
Dependency Injection
⚠️ BottleneckClasspath Scanning and Bean Creation can slow startup if too many generic @Component beans exist.
Optimization Tips
1Use @Service, @Repository, and @Controller to clearly define bean roles and optimize scanning.
2Avoid using @Component for all classes to reduce startup overhead.
3Enable debug logs to monitor bean scanning and startup performance.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using @Service instead of @Component for service classes?
ASpring applies service-specific optimizations reducing startup time.
BIt reduces network latency during requests.
CIt decreases CSS rendering time in the browser.
DIt automatically caches all service methods.
DevTools: Spring Boot Actuator / Logs
How to check: Enable debug logging for 'org.springframework.context' to see bean scanning details during startup.
What to look for: Look for the number of beans created and scanning duration to confirm optimized startup.