SteelReed

Search

Search posts, questions…

Spring BootQuestion 3 / 26

Junior

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.

scans downscans downout of rangecom.shop.Application@SpringBootApplicationcom.shop.ordercom.shop.paymentcom.common.AuditServiceNo bean found

3 points to say:

  1. Scanning finds stereotype-annotated classes in the given packages and creates beans.
  2. Spring Boot scans from the @SpringBootApplication class's package downward.
  3. Bean out of range: move the main class, use @Import or scanBasePackages; 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.