Backend Development 5 min read

Why IDEA Warns on @Autowired but Not on @Resource: A Spring DI Overview

IDEA flags @Autowired field injection but not @Resource because Spring advises against field injection—seeing it as tightly coupled and hard to test—while @Resource is a standard JSR‑250 annotation, so the IDE applies the warning only to the Spring‑specific @Autowired usage.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Why IDEA Warns on @Autowired but Not on @Resource: A Spring DI Overview

When using IDEA for Spring development, a warning appears on fields annotated with @Autowired : "Field injection is not recommended". The same warning does not show for @Resource . This article explains the reason and compares the two annotations.

Spring supports three common DI methods: constructor injection, setter injection, and field injection. Field injection can be done with either @Autowired (defined by Spring) or @Resource (defined by JSR‑250).

Key differences :

Dependency resolution : @Autowired defaults to byType (name can be forced with @Qualifier ); @Resource defaults to byName and falls back to byType if no name match.

Applicable targets : @Autowired works on constructors, methods, parameters, and fields; @Resource works only on methods and fields.

Provider : @Autowired is Spring‑specific, while @Resource is part of the Java standard (JSR‑250).

Why IDEA only warns on @Autowired : The warning reflects Spring’s recommendation to avoid field injection because it tightly couples the component to the IoC container, makes unit testing harder, and hides dependencies. @Resource is seen as a standard Java annotation, so IDEA does not apply the same Spring‑specific recommendation.

In practice, field injection is convenient but should be used sparingly; constructor injection is preferred for mandatory, immutable dependencies, while setter injection suits optional or mutable ones.

JavaSpringdependency injectionAutowiredIDEAResource
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

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.