When Should You Use @Autowired vs @Resource? A Deep Dive into Spring Dependency Injection

Spring developers often wonder whether to use @Autowired or @Resource for dependency injection; this article explains their origins, supported injection styles, configurable attributes, bean lookup strategies, and why constructor injection is generally preferred despite IDE warnings about field injection.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
When Should You Use @Autowired vs @Resource? A Deep Dive into Spring Dependency Injection

Warning

Recently a new teammate used @Autowired for dependency injection and got a yellow warning in IntelliJ IDEA, while @Resource did not. The author shares insights on this situation.

Difference between @Autowired and @Resource

Origin

@Resource is defined in JSR‑250 (2006).

@Autowired was introduced in Spring 2.5 (2007).

@Resource is a standard JSR, while @Autowired is Spring‑specific. Because @Autowired appeared later, it must provide something that @Resource cannot express.

Usage

Injection styles

Developers most often use field injection, but constructor injection and setter injection also exist. @Autowired supports constructor injection; @Resource does not.

Configurable attributes

@Autowired

supports the required attribute.

@Resource

provides seven additional attributes (name, type, etc.).

Bean lookup strategy

@Autowired

: type‑first; if multiple beans of the same type exist, it falls back to bean name. @Resource: name‑first; if the specified name is not found, it searches by type.

Lookup process

@Autowired

@Resource

Thoughts

For most developers the differences are minor. Although @Autowired adds constructor injection, field injection is already very convenient. The main distinction lies in the bean lookup strategy: @Autowired defaults to byType, while @Resource defaults to byName, giving @Resource a more deterministic resolution.

Conclusion

IDE warns against field injection (“Field injection is not recommended”). Switching to @Resource only silences the warning without solving the underlying issue. The recommended practice is to use constructor injection (optionally with Lombok) to keep class responsibilities clear.

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.

Javaspringdependency-injectionAutowiredresourceIDE Warning
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

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.