Loading published curriculum…

09 · Spring Framework and the IoC container / LESSON 38 OF 102

Component scanning and stereotype annotations

Component scanning discovers annotated classes within configured package boundaries.

Before this lesson: Inversion of control and ApplicationContext

Understand the topic

@Component is a general component marker; @Service communicates business responsibility; @Repository marks a persistence adapter and participates in exception translation when the corresponding infrastructure is present. @Controller belongs to the web layer.

Scanning too broadly can create accidental beans; scanning too narrowly can miss required services. Place a Boot application class above its application packages or configure scan boundaries explicitly. An annotation is metadata interpreted during context initialization.

EXAMPLE · JAVA

ScanConfig.java

import org.springframework.context.annotation.*;
@Configuration
@ComponentScan("com.example.tasks")
public class ScanConfig {}

What to expect

Loading this configuration scans com.example.tasks and descendants for component candidates; it does not scan every package on the machine.

YOUR TURN

Put it into practice

  1. 01Register a service through component scanning.
  2. 02Move it outside the scan root, reproduce the missing-bean error, then repair the boundary.
  3. 03Verify: The final package structure makes managed classes discoverable without scanning unrelated libraries.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

Component scanning discovers annotated classes within configured package boundaries.

  1. 01@Component is a general component marker; @Service communicates business responsibility; @Repository marks a persistence adapter and participates in exception translation when the corresponding infrastructure is present. @Controller belongs to the web layer.
  2. 02Scanning too broadly can create accidental beans; scanning too narrowly can miss required services. Place a Boot application class above its application packages or configure scan boundaries explicitly. An annotation is metadata interpreted during context initialization.
  3. 03Trace the example: Loading this configuration scans com.example.tasks and descendants for component candidates; it does not scan every package on the machine.

A mistake worth avoiding

A service annotation is present but no bean is found.

DEBUGGING ROOM

Something broke. Where do you look?

A service annotation is present but no bean is found.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Does @Service alone guarantee discovery outside the scan boundary?

Choose one answer

BUILD IT YOURSELF

Register a service through component scanning.

  1. 1.Register a service through component scanning.
  2. 2.Move it outside the scan root, reproduce the missing-bean error, then repair the boundary.
  3. 3.Reproduce this failure, then repair it: A service annotation is present but no bean is found.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Does @Service alone guarantee discovery outside the scan boundary?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

The final package structure makes managed classes discoverable without scanning unrelated libraries.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references