Spring Stereotype Annotations, Dependency Injection, and IoC Container
Stereotype Annotations
Section titled “Stereotype Annotations”All of these are stereotype annotations discovered through component scanning.
| Annotation | Layer | Purpose |
|---|---|---|
@Component |
Generic | Any Spring bean |
@Service |
Service | Business logic |
@Repository |
DAO | Database operations |
@Controller |
Web | Handles HTTP requests |
@RestController |
Web | REST API controller (@Controller + @ResponseBody) |
flowchart TD A[Component Scan] --> B[@Component] A --> C[@Service] A --> D[@Repository] A --> E[@Controller] B --> F[ApplicationContext] C --> F D --> F E --> F
Extra Feature
Section titled “Extra Feature”@Repository translates persistence exceptions:
SQLException -> DataAccessExceptionExample:
@Servicepublic class UserService {}
@Repositorypublic class UserRepository {}
@Controllerpublic class UserController {}Dependency Injection (DI)
Section titled “Dependency Injection (DI)”Dependency Injection is a design pattern where Spring provides dependencies instead of classes creating them.
Without DI:
Service service = new Service();With DI:
@AutowiredService service;Types of Dependency Injection
Section titled “Types of Dependency Injection”- Constructor Injection
- Setter Injection
- Field Injection
DI Flow
Section titled “DI Flow”sequenceDiagram
participant Client
participant Spring
participant Service
Client->>Spring: Request bean
Spring->>Service: Create object
Spring->>Service: Inject dependencies
Spring-->>Client: Return fully initialized bean
Constructor Injection vs Setter Injection
Section titled “Constructor Injection vs Setter Injection”| Feature | Constructor Injection | Setter Injection |
|---|---|---|
| Injection | Constructor | Setter |
| Mandatory Dependency | Yes | No |
| Immutability | Yes | No |
| Testing | Easier | Harder |
| Recommended | Yes | Less preferred |
Constructor injection:
@Servicepublic class OrderService {
private final PaymentService paymentService;
public OrderService(PaymentService paymentService) { this.paymentService = paymentService; }}Setter injection:
@Servicepublic class OrderService {
private PaymentService paymentService;
@Autowired public void setPaymentService(PaymentService paymentService) { this.paymentService = paymentService; }}Recommendation: constructor injection is preferred because it guarantees mandatory dependencies and supports immutable classes — see also the field-injection best practice note.
IoC Container
Section titled “IoC Container”The Inversion of Control (IoC) container is responsible for:
- Creating objects
- Injecting dependencies
- Managing bean lifecycle
- Configuring beans
Types of IoC Containers
Section titled “Types of IoC Containers”- BeanFactory
- Basic container
- Lazy initialization
- ApplicationContext
- Advanced container
- Supports AOP, events, and internationalization
Example:
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);@Bean vs @Component
Section titled “@Bean vs @Component”| Feature | @Component |
@Bean |
|---|---|---|
| Annotation Type | Class-level | Method-level |
| Bean Creation | Automatic | Manual |
| Location | On class | Inside @Configuration |
| Control | Less | More |
@Component
Section titled “@Component”@Componentpublic class EmailService {}@Configurationpublic class AppConfig {
@Bean public EmailService emailService() { return new EmailService(); }}XML Configuration (Legacy)
Section titled “XML Configuration (Legacy)”Before annotation-based configuration became standard, beans were declared in XML:
<bean id="emailService" class="com.example.EmailService"/>You’ll still encounter this in older Spring (non-Boot) codebases.
When to Use @Bean
Section titled “When to Use @Bean”- Third-party libraries
- Classes that cannot be modified
Recommendation: use @Component for application classes discovered by component scanning, and @Bean when you need explicit control or are configuring third-party classes.
Key Takeaways
Section titled “Key Takeaways”@Repositoryisn’t just a naming convention — it also translatesSQLExceptioninto Spring’sDataAccessExceptionhierarchy.- Constructor injection is preferred over setter/field injection: it guarantees mandatory dependencies, supports immutability, and is easier to test.
ApplicationContextis the container to use in practice;BeanFactoryis the more basic, lazily-initialized base container.- Use
@Componentfor your own classes and@Beanwhen you need explicit control or are wiring up a third-party class you can’t annotate.