Backend Development 7 min read

Spring Bean Injection Methods: Property, Setter, and Constructor Injection

This article explains Spring's three bean injection approaches—property, setter, and constructor injection—detailing their syntax, advantages, and drawbacks, referencing official Spring documentation to show the evolution from setter to constructor injection and advising developers on best practices.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Spring Bean Injection Methods: Property, Setter, and Constructor Injection

1. Injection Methods Overview

Spring provides three primary ways to inject bean instances: property injection, setter method injection, and constructor injection.

1.1 Property Injection

Property injection is the most common method, using annotations such as @Autowired , @Resource , or @Inject directly on a field.

@Service
public class BService {
    @Autowired
    AService aService;
    // ...
}

IDEA may warn against this approach, and it is generally not recommended because the class becomes tightly coupled to the Spring container.

1.2 Setter Method Injection

Setter injection is rarely used due to its verbosity.

@Service
public class BService {
    AService aService;

    @Autowired
    public void setaService(AService aService) {
        this.aService = aService;
    }
}

The code is considered cumbersome and is discouraged.

1.3 Constructor Injection

Constructor injection is preferred in modern Spring versions. If a class has a single constructor, the @Autowired annotation can be omitted; otherwise it must be present to indicate which constructor to use.

@Service
public class AService {
    BService bService;
    @Autowired
    public AService(BService bService) {
        this.bService = bService;
    }
}

2. Comparison of Injection Methods

Spring’s official documentation from version 3.0 advocated setter injection for optional dependencies, but from version 4.0 onward the team recommends constructor injection. The benefits of constructor injection include immutable dependencies, guaranteed non‑null injection, and fully initialized objects after construction.

When a constructor has many parameters, it may indicate a design issue that violates the single‑responsibility principle.

3. Drawbacks of Property Injection

Property injection relies on reflection and cannot be used outside the IoC container, making the class difficult to instantiate manually.

4. Summary

The article provides a practical overview of Spring’s injection mechanisms, showing code examples, historical documentation references, and best‑practice recommendations for choosing the appropriate injection style.

backendJavaSpringDependency InjectionConstructor InjectionSetter InjectionProperty Injection
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.