Understanding @Autowired, @Resource, and @Inject: When and How to Use Spring DI Annotations

This article explains the differences between Spring's @Autowired, @Resource, and @Inject annotations, describes the injection order and rules, shows code examples for each approach, and discusses why constructor‑based injection is recommended over field injection for cleaner, more maintainable applications.

Programmer DD
Programmer DD
Programmer DD
Understanding @Autowired, @Resource, and @Inject: When and How to Use Spring DI Annotations

Preface

This chapter explores several key points about dependency injection in Spring development.

@Autowired , @Resource , @Inject three annotations differences

When using @Autowired , have you ever seen the warning "Field injection is not recommended"? Do you know why?

What are the injection methods in Spring and what does the official recommendation say?

If you already understand these questions, your development experience is likely solid.

@Autowired, @Resource, @Inject differences

Spring supports the three annotations for dependency injection. Below we explain their differences.

@Autowired

@Autowired is provided by the Spring framework and requires importing org.springframework.beans.factory.annotation.Autowired.

Example code:

public interface Svc { void sayHello(); }@Service public class SvcA implements Svc { @Override public void sayHello() { System.out.println("hello, this is service A"); } }@Service public class SvcB implements Svc { @Override public void sayHello() { System.out.println("hello, this is service B"); } }@Service public class SvcC implements Svc { @Override public void sayHello() { System.out.println("hello, this is service C"); } }

Test class:

@SpringBootTest public class SimpleTest { @Autowired Svc svc; @Test void rc() { Assertions.assertNotNull(svc); svc.sayHello(); } }

Injection order:

Search for a bean matching the type in the context.

If multiple beans exist, match by name.

If @Qualifier is present, match the name specified by the qualifier.

If no qualifier, match the variable name.

If no match is found, an error is thrown (unless @Autowired(required=false) is used).

@Inject

In a Spring environment, @Inject and @Autowired are equivalent because both are processed by AutowiredAnnotationBeanPostProcessor. @Inject is defined by JSR‑330 and can also be used with Guice.

Key differences: @Inject comes from the Java EE package and does not have a required attribute, whereas @Autowired can set required=false.

@Resource

@Resource

is defined by JSR‑250 and is processed by CommonAnnotationBeanPostProcessor. It has two important attributes: name and type. Spring resolves name to the bean name and type to the bean type.

Injection order when both name and type are specified:

If both are specified, Spring finds a unique bean that matches both; otherwise an exception is thrown.

If only name is specified, Spring looks for a bean with that name.

If only type is specified, Spring looks for a unique bean of that type; multiple or zero matches cause an exception.

If neither is specified, Spring first tries byName and then byType.

IDEA warning "Field injection is not recommended"

When using IDEA for Spring development, adding @Autowired on a field triggers a warning.

Field injection is not recommended Inspection info: Spring Team Recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".

Convert field injection to constructor injection using Alt+Enter:

@Service public class HelpService { private final Svc svc; @Autowired public HelpService(@Qualifier("svcB") Svc svc) { // Assert.notNull(svc, "svc must not be null"); this.svc = svc; } public void sayHello() { svc.sayHello(); } }

Spring team advice:

Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies.

Benefits of constructor injection: creates immutable objects, guarantees required dependencies are non‑null, and ensures beans are fully initialized. A large number of constructor parameters is a code smell indicating too many responsibilities.

Setter injection should be used only for optional dependencies that can have reasonable default values; otherwise null‑checks proliferate throughout the code.

Summary

Understanding the three Spring DI annotations, their injection order, and the reasons why constructor injection is preferred helps you write cleaner, more maintainable Spring applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

springdependency-injectionConstructor InjectionField InjectionAutowiredresourceInject
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

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.