How does component scanning work? Why does Spring say a bean is missing even though the class has @Service?
Short answer
Component scanning walks the classpath, finds classes with @Component (and @Service, @Repository…) and registers them as beans. Spring Boot scans from the main class's package downward; a "bean not found" usually means the class sits outside that range.
3 points to say:
- Scanning finds stereotype-annotated classes in the given packages and creates beans.
- Spring Boot scans from the
@SpringBootApplicationclass's package downward. - Bean out of range: move the main class, use
@ImportorscanBasePackages; shared libraries should use auto-configuration, not scanning.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.