Spring @Autowired Annotation: Constructor, Setter & Field Injection Guide (2025)

The Spring @Autowired annotation is one of the core annotations in Spring Framework used for automatic Dependency Injection (DI).

If you're developing applications with Spring Boot, understanding this concept is essential. Instead of manually managing dependencies between classes, @Autowired delegates that responsibility to the Spring Container.

In this guide:


What Is the Spring @Autowired Annotation?

@Autowired tells Spring to automatically inject a managed bean into another bean.

Spring Framework is built on the Dependency Injection principle. Rather than creating their own dependencies, classes let the Spring Container supply them.

@Autowired = "Let Spring inject this dependency for me"

@Autowired can be applied in three places:

  • Field injection — most common, least recommended
  • Setter injection — for optional dependencies
  • Constructor injection — Spring's recommended approach

How to Enable @Autowired in Spring Boot?

In Spring Boot applications, no extra configuration is needed. The @SpringBootApplication annotation enables automatic component scanning, which detects and registers Spring-managed beans.

@SpringBootApplication
public class EgitimApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(EgitimApplication.class, args);
    }
}

With this setup:

  • Classes annotated with @Service, @Repository, or @Component in the same package (and sub-packages) are registered as beans
  • These beans can then be injected anywhere using @Autowired

Types of @Autowired Injection

1. Constructor Injection — Recommended by Spring

Constructor injection is the most recommended method of dependency injection in Spring. It makes dependencies mandatory and produces an immutable design.

@Service
@Transactional
public class AccountService {

    private final UserRepository userRepository;

    @Autowired
    public AccountService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}
💡 Spring Boot tip: If there is only one constructor, @Autowired is optional — Spring detects and uses it automatically.

Advantages:

  • Dependencies are mandatory — reduces null pointer risk
  • Supports immutable design — allows use of final
  • Increases testability — easy to mock dependencies without Spring context

2. Setter Injection

Setter injection is appropriate when a dependency is optional or needs to be changed after object creation.

@Service
@Transactional
public class AccountService {

    private UserRepository userRepository;

    @Autowired
    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}

Not recommended for required dependencies — the object can be created without the dependency being injected.


3. Field Injection — Avoid in Production Code

Field injection is the most common but least recommended approach. It makes testing harder and hides dependencies.

@Service
@Transactional
public class AccountService {

    @Autowired
    private UserRepository userRepository;
}

Disadvantages:

  • Makes unit testing difficult — requires a Spring context to work
  • Hides dependencies — reduces code readability
  • Cannot use final — immutability is not possible

Common @Autowired Mistakes

1. Multiple Beans of the Same Type — Use @Qualifier

By default, Spring injects beans by type. If multiple beans of the same type exist, Spring doesn't know which one to inject and throws:

NoUniqueBeanDefinitionException: expected single matching bean but found multiple

Use @Qualifier to specify which bean to inject:

@Autowired
@Qualifier("userRepositoryImpl")
private UserRepository userRepository;

2. Why Is @Autowired Not Working? — The new Keyword Mistake

If you create a class with the new keyword, Spring does not manage that instance and dependency injection will not occur — injected fields will remain null.

Solution:

  • Annotate the class with @Component, @Service, or @Repository
  • Always obtain instances through the Spring Context — never use new

Conclusion: Which Injection Type Should You Use?

In this guide, we covered what Spring @Autowired is, how it works, and compared all three injection types.

Summary:

  • Constructor Injection → for required dependencies — best practice
  • Setter Injection → for optional dependencies — use when needed
  • Field Injection → convenient but avoid in production code

Frequently Asked Questions (FAQ)

What is @Autowired in Spring?

@Autowired is an annotation used to perform automatic Dependency Injection in Spring Framework. The Spring Container finds the matching bean and injects it automatically.

What should I use instead of @Autowired?

Constructor injection is the preferred approach. In Spring Boot, if the class has only one constructor, @Autowired is not even required.

Is @Autowired mandatory in Spring Boot?

No. If a class has only one constructor, Spring automatically performs injection without the @Autowired annotation.

Why is field injection not recommended?

Field-injected dependencies are null outside the Spring Context, making unit tests difficult. It also prevents the use of the final keyword, which means you lose immutability guarantees.


Related Posts

Last Updated: 2025

Latest Software Developers - Yazılım Blog Yazarı Profil Resmi

Author

LatestSoftwareDevelopers

Blog where the most up-to-date software is followed.

Comments on Java

Leave a Reply

Your email address will not be published. Required fields are marked * *