---
title: "Spring doesn’t recommend @Autowired anymore???"
source: "https://medium.com/@ByteCodeBlogger/spring-doesnt-recommend-autowired-anymore-895987b2e2f3"
author:
  - "[[Full Stack Developer]]"
published: 2024-07-19
created: 2024-10-29
description: "In Spring, dependency injection is a fundamental concept used to manage the lifecycle and dependencies of beans. @Autowired is a common way to inject dependencies into Spring-managed components…"
tags:
  - "clippings"
---
[

![Full Stack Developer](https://miro.medium.com/v2/resize:fill:88:88/1*ANzDaum1wnT55zhf1sbLSg@2x.jpeg)

](https://medium.com/@ByteCodeBlogger?source=post_page---byline--895987b2e2f3--------------------------------)

There are 3 ways we can inject dependencies and Field Injection is not recommended anymore.

![](https://miro.medium.com/v2/resize:fit:700/0*WeheP9k6lgJm-ZVv)

Photo by [Luigi Pozzoli](https://unsplash.com/@lu_pl_ph?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com/?utm_source=medium&utm_medium=referral)

In Spring, dependency injection is a fundamental concept used to manage the lifecycle and dependencies of beans. `@Autowired` is a common way to inject dependencies into Spring-managed components.

> When we run the code analysis tool in the IDE, it may issue the “Field injection is not recommended” warning for fields with the @Autowired annotation

However, there are other ways to achieve the same goal, and Spring has evolved its best practices over time. Here are the different methods and the reasons why Spring might not recommend `@Autowired` as strongly anymore:

## 3 Methods to Autowire in Spring

## **Constructor Injection**:

```
@Servicepublic class MyService {    private final MyRepository myRepository;    public MyService(MyRepository myRepository) {        this.myRepository = myRepository;    }}
```

**Pros**:

- Encourages immutability and makes dependencies explicit.
- Ensures that the bean is fully initialized with its required dependencies.
- Easier to write unit tests for.

## **2\. Setter Injection**:

```
@Servicepublic class MyService {    private MyRepository myRepository;    @Autowired    public void setMyRepository(MyRepository myRepository) {        this.myRepository = myRepository;    }}
```

**Pros**:

- Allows for optional dependencies.
- Can be useful when you need to change dependencies after bean creation.

## **3 . Field Injection**:

```
@Servicepublic class MyService {    @Autowired    private MyRepository myRepository;}
```

**Pros**:

- Simplifies the code by reducing boilerplate.

**Cons**:

- Difficult to test.
- Harder to see the required dependencies.
- Violates the principle of immutability.

## Why Spring Might Not Recommend `@Autowired` Anymore

1. **Constructor Injection Preferred**:

- **Explicit Dependencies**: Constructor injection makes it clear what the dependencies are, improving readability and maintainability.
- **Immutability**: Encourages immutability by ensuring that dependencies are final and cannot be changed after bean creation.
- **Ease of Testing**: Makes unit testing easier since dependencies are passed in through the constructor, allowing for easier mocking.

**2\. Field Injection Downsides**:

- **Hidden Dependencies**: Dependencies are not explicit in the class’s constructor, making it harder to understand the required dependencies at a glance.
- **Testing Difficulty**: It is more difficult to inject mock dependencies into private fields for unit tests.
- **Reflection Use**: Requires the use of reflection to set private fields, which can have performance implications and complicate debugging.

**3\. Setter Injection Use Cases**:

- **Optional Dependencies**: Setter injection is useful when some dependencies are optional and not required for the bean’s initial creation.
- **Configuration Changes**: Allows changing dependencies after the bean has been created, which can be useful in certain scenarios.

## Conclusion

While `@Autowired` is still commonly used and perfectly valid, Spring's evolving best practices lean towards constructor injection for the reasons mentioned above. Constructor injection leads to better designed, more maintainable, and testable code by making dependencies explicit and encouraging immutability.