juniorDependency Injection
What is Setter (Property) Injection?
Updated Apr 28, 2026
Short answer
Setter Injection provides dependencies through public properties or setter methods after the object has been constructed.
Deep explanation
Unlike constructor injection, setter injection allows dependencies to be optional or changed after initialization. However, it leaves the object in a potentially invalid state right after construction if the dependency is required but hasn't been set yet.
Real-world example
Applying an optional theme or logger to a UI component that has a reasonable default behavior.
Common mistakes
- Using setter injection for required dependencies, leading to unpredictable null errors.
Follow-up questions
- Is Setter injection thread-safe?