Injecting Specific Service Implementations in Spring with @Autowired, @Qualifier, and @Resource

This article explains three methods to inject a specific service implementation into a Spring controller—using @Autowired with @Qualifier, using @Resource with type, and using named @Service beans combined with @Resource(name), including code examples and differences between @Autowired and @Resource.

Top Architect
Top Architect
Top Architect
Injecting Specific Service Implementations in Spring with @Autowired, @Qualifier, and @Resource

Spring provides several ways to inject a specific implementation of a service interface into a controller. This article describes three common approaches.

Method 1 : Use @Autowired together with @Qualifier("beanId") to specify which bean to inject.

public interface HumanService {
    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 {
    @Autowired
    @Qualifier("teacherServiceImpl")
    private HumanService humanService;

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

Method 2 : Use @Resource(type = DoctorServiceImpl.class) (or @Resource(name="...")) to inject by type or name.

@RestController
public class HumanController {
    // @Resource(type = DoctorServiceImpl.class) // method 2
    @Autowired
    @Qualifier("teacherServiceImpl")
    private HumanService humanService;
    // ...
}

Method 3 : Define bean names in @Service("beanName") and inject them with @Resource(name="beanName").

@Service("teacherService")
public class TeacherServiceImpl implements HumanService { /* ... */ }
@Service("doctorService")
public class DoctorServiceImpl implements HumanService { /* ... */ }

@RestController
public class HumanController {
    @Resource(name="doctorService")
    private HumanService humanService;
    // ...
}

The article also explains that @Service registers the class as a bean, that @Autowired performs by‑type injection by default, while @Resource defaults to by‑name injection, and notes differences such as required‑attribute handling.

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.

javaspringannotationsdependency-injection
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn 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.