Choosing the Right Spring Bean Injection: @Autowired vs @Resource vs @Qualifier
This article explains three practical ways to specify which Spring bean to inject in a controller, compares @Autowired and @Resource annotations, clarifies the role of @Service, and provides complete Java code examples for each approach.
Three ways to specify which bean to inject in a Spring controller
Method 1: Use @Autowired together with @Qualifier("beanId") to indicate the exact bean.
Method 2: Use @Resource(type = ClassName.class) to specify the bean by its class.
Method 3: Give each @Service implementation a name (e.g., @Service("teacherService")) and inject it with @Resource(name="teacherService") or @Autowired @Qualifier("teacherService").
What @Service actually does
1. Declares the class as a Spring bean, allowing other components to autowire it.
2. Registers the bean with an id equal to the class name with the first letter lower‑cased (e.g., teacherServiceImpl).
Note: Bean names must be unique; duplicate names cause errors.
Meaning of @Autowired
When Spring encounters @Autowired, it searches the application context for a matching bean (by type by default) and injects it.
Meaning of @Resource
@Resourceperforms the same injection as @Autowired but follows J2EE semantics.
Differences between @Autowired and @Resource
@Autowiredis a Spring annotation; @Resource is a J2EE annotation. @Autowired matches beans by type by default; @Resource matches by name by default. @Autowired requires the dependency to exist; you can make it optional with @Autowired(required=false).
Example code
Interface:
public interface HumanService {
String name();
}Implementations:
@Service
public class TeacherServiceImpl implements HumanService {
@Override
public String name() {
System.out.println("teacher");
return "teacher";
}
}
@Service
public class DoctorServiceImpl implements HumanService {
@Override
public String name() {
System.out.println("doctor");
return "doctor";
}
}Controller using @Qualifier:
@RestController
public class HumanController {
@Autowired
@Qualifier("teacherServiceImpl")
private HumanService humanService;
@RequestMapping("/name")
public String name() {
return humanService.name();
}
}Controller using @Resource:
@RestController
public class HumanController {
@Resource(name="doctorService")
private HumanService humanService;
@RequestMapping("/name")
public String name() {
return humanService.name();
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack 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.
