Component Scanning
How Component Scanning Works
com.example.app/ ← @SpringBootApplication here
├── controller/ ← Scanned ✓
│ └── ProductController
├── service/ ← Scanned ✓
│ └── ProductService
├── repository/ ← Scanned ✓
│ └── ProductRepository
└── util/ ← Scanned ✓
└── Helper
What Gets Scanned
| Annotation | Package | Purpose |
|---|---|---|
@Component |
Any | Generic Spring-managed component |
@Service |
Service layer | Business logic |
@Repository |
Data layer | Database access |
@Controller |
Web layer | MVC controllers |
@RestController |
Web layer | REST API controllers |
@Configuration |
Config | Bean definitions |
Custom Scan Paths
@SpringBootApplication
@ComponentScan(basePackages = "com.example.core")
public class Application { }
Filtering
@ComponentScan(
basePackages = "com.example",
includeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = CustomAnnotation.class
),
excludeFilters = @ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
classes = TestHelper.class
)
)
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 Scanning 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 Scanning in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Component Scanning implementation
// Key aspects: validation, error handling, logging, testing
public class ComponentScanning {
// Production-ready implementation
}Identify and handle edge cases for Component Scanning. 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 Scanning. 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. Where does component scanning start by default?
2. Which annotation is a specialization of @Component for services?
3. What is the primary purpose of Component Scanning?
4. What is a common mistake when implementing Component Scanning?
Flashcards
Question
Where does component scanning start?
Click to reveal answer
Answer
Package of @SpringBootApplication class + all sub-packages
Question
Stereotype annotations for components?
Click to reveal answer
Answer
@Component, @Service, @Repository, @Controller, @RestController
Question
What is Component Scanning?
Click to reveal answer
Answer
Component Scanning is a key concept in backend development.
Question
When to use Component Scanning?
Click to reveal answer
Answer
Use Component Scanning when building production systems that require reliability, scalability, and maintainability.
Question
Component Scanning 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 scanning starts from @SpringBootApplication package
- 2.Stereotype annotations: @Component, @Service, @Repository, @Controller
- 3.Custom scan paths with @ComponentScan(basePackages)
- 4.All sub-packages are scanned automatically
Interview Tips
- •Explain component scanning behavior
- •Know stereotype annotations
Cheat Sheet
Component Scanning
- Start: Package of @SpringBootApplication
- Scope: All sub-packages
- Stereotypes: @Component, @Service, @Repository, @Controller
- Custom: @ComponentScan(basePackages = "...")