Dependency Injection Articles
Articles covering the fundamentals of dependency injection and bean design as the foundation for understanding Spring Boot.
-
Differences Between @Service, @Repository, and @Controller in Spring Boot and How to Use Them - From Their Relationship with @Component to Exception Translation
@Service, @Repository, and @Controller are all derived from @Component, but they differ in actual effects beyond DI registration, such as exception translation in @Repository and handler mapping in @Controller. This article demonstrates the differences with source code and verification code, and explains the criteria for choosing annotations for each layer with a quick reference table.
-
Thorough Comparison of Spring Boot's 3 Dependency Injection Methods - When to Use Constructor, Setter, and Field Injection
Compare the differences between Spring Boot's constructor, setter, and field injection from the perspectives of testability, null safety, and circular dependency detection. Explains combinations with @RequiredArgsConstructor and workarounds for circular dependencies with practical examples.
-
How to Reduce Boilerplate Code with Lombok in Spring Boot
A practical explanation of the roles and proper usage of Lombok annotations frequently used in Spring Boot development (@Data, @Builder, @RequiredArgsConstructor, @Slf4j, etc.). Also covers pitfalls when used with JPA Entity and how to combine it with constructor injection.
-
Understanding Spring Boot Bean Scopes - When to Use singleton, prototype, request, and session
A thorough guide to the behavior and use cases of Spring Boot's five Bean scopes (singleton/prototype/request/session/application). Covers the pitfall where @Scope("prototype") still returns the same instance, and four workarounds using ObjectProvider, @Lookup, and jakarta.inject.Provider with implementation examples.
-
Understanding Spring Boot Bean Lifecycle - How to Use @PostConstruct, @PreDestroy, and InitializingBean
A visual walkthrough of the Spring Bean lifecycle from creation through initialization to destruction, explaining how to choose between four implementation patterns — @PostConstruct, @PreDestroy, InitializingBean, and DisposableBean — based on your use case.
-
Spring @Bean "Name" Guide - When to Set It? How Does It Work? What's the Priority?
A practical guide to the role of the 'name' attribute on Spring Boot's @Bean. Covers default naming conventions (method name = Bean name), how to assign explicit names/aliases, the priority of @Qualifier, @Primary, and parameter names, avoiding Bean name collisions, and tips for using constants, all with code examples.
-
What Is @Component in Spring Boot? Differences from @Bean and How to Use It
A beginner-friendly guide to @Component in Spring Boot. Learn the differences from @Bean, criteria for choosing between them, how it relates to @Service and @Repository, and practical usage including constructor injection, with code examples.
-
What Is DI (Dependency Injection) in Spring Boot? How It Works, How to Write It, and Its Benefits [Beginner's Guide]
A beginner-friendly explanation of DI (Dependency Injection) in Spring Boot. Learn why using new is not enough, how to write constructor injection, and concrete benefits such as easier testing, swappable implementations, and no unnecessary instances, all with sample code.