Spring Boot Beans and Components
A bean is any object that Spring creates and manages. Every major class in your app — controllers, services, repositories — becomes a bean. Spring keeps track of all beans in its container and injects them wherever needed.
The City Grid Analogy
Think of Spring's container as a city grid. Every building (bean) is registered on a map. When one building needs electricity from another, the city infrastructure (Spring) connects them. You do not run the wires — Spring does.
Stereotype Annotations
Spring uses stereotype annotations to identify which classes should become beans. Each annotation carries meaning about what the class does.
Annotation Layer Purpose ─────────────── ───────────── ────────────────────────────────── @Component Any layer Generic bean — no specific role @Controller Web layer Handles HTTP requests, returns views @RestController Web layer @Controller + @ResponseBody combined @Service Business layer Contains business logic @Repository Data layer Handles database operations
Hierarchy of Stereotype Annotations
@Component
(parent of all)
/ | \
@Controller @Service @Repository
│
@RestController
(= @Controller + @ResponseBody)
All stereotypes extend @Component. The difference is semantic — @Service and @Repository tell the reader (and Spring) what role the class plays.
Defining Beans with @Component
@Component
public class EmailSender {
public void send(String to, String message) {
System.out.println("Sending email to " + to);
}
}
Spring scans your package, finds this class, creates one instance, and registers it as a bean. Any other class can receive this bean via injection.
Defining Beans with @Bean in @Configuration
Use @Bean inside a @Configuration class when you need to configure a third-party class that you cannot annotate directly:
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(); ← Spring manages this object
}
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
return mapper;
}
}
RestTemplate is a third-party class — you cannot add @Component to it. The @Bean method lets you create it and hand it to Spring.
Bean Scopes
By default, Spring creates one instance of each bean and reuses it throughout the app. This is the Singleton scope.
Scope Instances Created When to Use ───────────── ─────────────────── ────────────────────────────── Singleton One per container Default — services, repos Prototype New one each time Stateful helper objects Request One per HTTP request Web apps only Session One per user session Web apps with login
@Component
@Scope("prototype")
public class ReportGenerator {
// A new instance is created every time this is injected
}
How Spring Finds Your Beans
@SpringBootApplication
│
└── @ComponentScan
│
▼
Scans: com.estudy247.demo (your base package)
│
▼
Finds every class with:
@Component / @Service / @Repository / @Controller
│
▼
Creates one bean per class
Stores all beans in ApplicationContext
Only classes inside the base package (and its sub-packages) are scanned. Place all your classes under the main class's package to ensure they are found.
Naming Beans
By default, the bean name equals the class name in camelCase. EmailSender becomes the bean named emailSender. You can set a custom name:
@Component("mailer")
public class EmailSender { ... }
@Primary and @Qualifier
When two beans share the same type, Spring does not know which one to inject. Use @Primary to mark the default, or @Qualifier to specify exactly which one you want:
@Service
@Primary
public class GmailSender implements EmailService { ... }
@Service
public class SendGridSender implements EmailService { ... }
// In the consumer:
@Autowired
@Qualifier("sendGridSender")
private EmailService emailService; ← Gets SendGridSender specifically
Summary
- A bean is an object created and managed by the Spring container
- Use
@Component,@Service,@Repository,@Controllerto register beans - Use
@Beanin a@Configurationclass for third-party objects - The default scope is Singleton — one instance shared across the app
@Primaryand@Qualifierresolve conflicts when multiple beans share a type
