Choosing Between @Autowired, @Resource, and @Qualifier for Service Injection in Spring

This article explains three ways to inject a specific service implementation in Spring—using @Qualifier with @Autowired, using @Resource with type or name attributes, and naming @Service beans—detailing their semantics, bean‑matching rules, and providing complete code examples for each approach.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Choosing Between @Autowired, @Resource, and @Qualifier for Service Injection in Spring

The article introduces three methods to specify which service implementation should be injected into a Spring controller.

Method 1: Use @Autowired together with @Qualifier("beanId") to indicate the exact bean to inject.

Method 2: Use @Resource(type = ClassName.class) (or @Resource(name="beanName")) to specify the target bean by type or name.

Method 3: Assign a name to each @Service implementation (e.g., @Service("teacherService")) and inject by that name with @Resource(name="teacherService") or @Qualifier("teacherService").

The @Service annotation registers the class as a Spring bean and sets its default bean ID to the class name with a lowercase first letter; naming it explicitly avoids conflicts.

The @Autowired annotation performs type‑based injection by default, while @Resource follows J2EE semantics and defaults to name‑based injection. @Autowired can be made optional by setting required=false.

Below are the complete code examples used in the article.

public interface HumanService {
    public String name();
}
@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";
    }
}
@RestController
public class HumanController {
    // Method 1 example
    @Autowired
    @Qualifier("teacherServiceImpl")
    private HumanService humanService;

    // Method 2 example (commented)
    // @Resource(type = DoctorServiceImpl.class)
    // private HumanService humanService;

    @RequestMapping("/name")
    public String name(){
        return humanService.name();
    }
}
@Service("teacherService")
public class TeacherServiceImpl implements HumanService {
    @Override
    public String name() {
        System.out.println("teacher");
        return "teacher";
    }
}

@Service("doctorService")
public class DoctorServiceImpl implements HumanService {
    @Override
    public String name() {
        System.out.println("doctor");
        return "doctor";
    }
}
@RestController
public class HumanController {
    @Resource(name="doctorService")
    private HumanService humanService;

    @RequestMapping("/name")
    public String name(){
        return humanService.name();
    }
}

Finally, the article reminds readers that bean names must be unique to avoid runtime errors.

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.

JavaBackend DevelopmentspringSpring Bootannotationsdependency-injection
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.