@Component
@Component
@Component
public class EmailValidator {
public boolean isValid(String email) {
return email != null && email.contains("@");
}
}
@Component vs Specialized Annotations
| Annotation | Layer | Use Case |
|---|---|---|
@Component |
Generic | Utility classes, general beans |
@Service |
Business | Business logic |
@Repository |
Data | Database access |
@Controller |
Web | MVC controllers |
When to Use @Component
Use @Component when the class doesn't fit into a specific layer:
// Generic component — no specific layer
@Component
public class PriceCalculator {
public BigDecimal calculateDiscount(BigDecimal price, int percent) {
return price.multiply(BigDecimal.valueOf(percent / 100.0));
}
}
// Service layer — use @Service
@Service
public class OrderService { ... }
// Data layer — use @Repository
@Repository
public class OrderRepository { ... }
Spring Best Practices
Configuration
- Use properties over YAML for simple configs
- Externalize configuration
- Use profiles for environments
- Validate on startup
Bean Management
- Prefer constructor injection
- Use appropriate scope
- Implement lazy initialization
- Clean up resources
Security
- Use Spring Security
- Implement CSRF protection
- Use method-level security
- Log security events
Key Points
- Understanding @Component Annotation is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for @Component Annotation in a backend system. Consider scalability, error handling, and production readiness.
Solution
// @Component Annotation implementation
// Key aspects: validation, error handling, logging, testing
public class ComponentAnnotation {
// Production-ready implementation
}Identify and handle edge cases for @Component Annotation. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for @Component Annotation. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. When should you use @Component instead of @Service?
2. What is the functional difference between @Component and @Service?
3. What is the primary purpose of @Component Annotation?
4. What is a common mistake when implementing @Component Annotation?
Flashcards
Question
When to use @Component?
Click to reveal answer
Answer
Generic beans that don't fit a specific layer (utility classes)
Question
@Component vs @Service difference?
Click to reveal answer
Answer
Functionally equivalent — @Service adds semantic meaning
Question
What is @Component Annotation?
Click to reveal answer
Answer
@Component Annotation is a key concept in backend development.
Question
When to use @Component Annotation?
Click to reveal answer
Answer
Use @Component Annotation when building production systems that require reliability, scalability, and maintainability.
Question
@Component Annotation best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.@Component is the base stereotype annotation
- 2.Use @Service for business logic, @Repository for data access
- 3.@Component/@Service are functionally equivalent
- 4.Use specialized annotations for clarity
Interview Tips
- •Know when to use each stereotype annotation
- •Explain the semantic difference
Cheat Sheet
@Component
- @Component: Generic Spring bean
- @Service: Business logic layer
- @Repository: Data access layer
- @Controller: Web MVC layer
- All are functionally equivalent — semantic difference only