Why IntelliJ IDEA Warns on @Autowired but Not on @Resource: Understanding Spring Dependency Injection
This article explains the differences between @Autowired and @Resource in Spring, why IDEA warns only on @Autowired field injection, and outlines the pros and cons of constructor, setter, and field injection methods.
When using IntelliJ IDEA, you may notice a warning on fields annotated with @Autowired that says "Field injection is not recommended", while @Resource does not trigger this warning.
Field injection is not recommended.
Most articles only compare the two annotations without explaining why; this post summarizes possible reasons.
Common Spring DI methods
Constructor injection : inject dependencies via constructor parameters.
Setter injection : inject via setter methods.
Field injection : inject directly on fields using @Autowired or @Resource .
@Autowired vs @Resource
Both perform dependency injection, but @Autowired is defined by Spring, whereas @Resource follows the JSR‑250 specification.
Dependency resolution : @Autowired defaults to by‑type (can use @Qualifier for name); @Resource defaults to by‑name, falling back to by‑type if no bean with that name is found.
Applicable targets : @Autowired can be used on constructors, methods, parameters, and fields; @Resource works on methods and fields only.
Provider : @Autowired is supplied by Spring; @Resource is supplied by the JSR‑250 standard.
Advantages and disadvantages of each DI style
According to the Spring documentation, the recommended usage scenarios are:
Constructor injection : strong dependency, immutable.
Setter injection : optional, mutable.
Field injection : should be avoided when possible; if used, @Resource couples less tightly to the IoC container than @Autowired .
Drawbacks of field injection
Cannot inject immutable objects as with constructor injection.
Dependencies are hidden from external view.
Creates tight coupling with the IoC container, making component reuse difficult.
Unit tests also need the IoC container.
When many dependencies are present, the constructor becomes bulky, indicating a possible violation of the Single Responsibility Principle.
Why does IDEA only warn on @Autowired?
Field injection is convenient, reducing boilerplate code, which is why many developers prefer it despite its drawbacks. However, @Autowired is a Spring‑specific annotation, tightly binding the code to the Spring IoC container; switching to another container may break the injection. In contrast, @Resource is part of the Java standard (JSR‑250), so containers are expected to support it, providing looser coupling.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.