Mastering Spring @Resource: Inject Maps, Lists, Arrays, and Single Beans
This guide demonstrates how to use Spring's @Resource annotation to inject collections such as Map, List, and array, as well as single beans, and explains resolution strategies like @Priority, @Primary, BeanFactory, ApplicationContext, @Qualifier, and custom qualifiers.
Environment: Spring 6.1.2
1. Prepare Environment
Define an interface DAO and three implementations: WorkerDAO, StudentDAO, and TeacherDAO.
public interface DAO {}
public class WorkerDAO implements DAO {}
public class StudentDAO implements DAO {}
public class TeacherDAO implements DAO {} PersonServiceinjects the DAO implementations.
public class PersonService {
@Resource
private Type daos;
public String toString() {
return "PersonService [daos=" + daos + "]";
}
}The main method registers beans, refreshes the context, and prints the PersonService instance.
public static void main(String[] args) {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.registerBean("worker", WorkerDAO.class);
context.registerBean("student", StudentDAO.class);
context.registerBean("teacher", TeacherDAO.class);
context.registerBean(PersonService.class);
context.refresh();
System.out.println(context.getBean(PersonService.class));
}
}2. Type Injection
2.1 Map injection
@Resource
private Map<String, DAO> daos;Output shows the map keyed by bean names.
PersonService [daos={worker=com.pack.WorkerDAO@..., student=com.pack.StudentDAO@..., teacher=com.pack.TeacherDAO@...}]2.2 List injection
@Resource
private List<DAO> daos;Output shows the list of DAO instances.
PersonService [daos=[com.pack.TeacherDAO@..., com.pack.StudentDAO@..., com.pack.WorkerDAO@...]]2.3 Array injection
@Resource
private DAO[] daos;Output is similar to the list injection.
PersonService [daos=[com.pack.TeacherDAO@..., com.pack.StudentDAO@..., com.pack.WorkerDAO@...]]2.4 Single bean injection
@Resource
private DAO daos;When multiple DAO beans exist, Spring throws NoUniqueBeanDefinitionException.
NoUniqueBeanDefinitionException: No qualifying bean of type 'com.pack.DAO' available: expected single matching bean but found 3: worker,student,teacherResolution methods:
@Priority – assign priority values; lower number wins.
@Priority(-1)
public class WorkerDAO implements DAO {}
@Priority(-2)
public class StudentDAO implements DAO {}
@Priority(-3)
public class TeacherDAO implements DAO {}After setting priorities, TeacherDAO is injected.
PersonService [daos=com.pack.TeacherDAO@...]@Primary – mark the preferred bean.
@Primary
public class WorkerDAO implements DAO {}Now WorkerDAO is injected.
PersonService [daos=com.pack.WorkerDAO@...]Other ways to obtain a specific bean manually:
Using
BeanFactory public class PersonService implements BeanFactoryAware {
private DAO daos;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.daos = beanFactory.getBean("student", StudentDAO.class);
}
}Using
ApplicationContext public class PersonService implements ApplicationContextAware {
private DAO daos;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.daos = context.getBean("student", StudentDAO.class);
}
}Using @Qualifier to specify bean name
@Resource
@Qualifier("student")
private DAO daos;Custom qualifier annotation example:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
public @interface PackInject {}Register the custom qualifier with Spring:
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
CustomAutowireConfigurer configurer = new CustomAutowireConfigurer();
configurer.setCustomQualifierTypes(Set.of(PackInject.class));
context.registerBean(CustomAutowireConfigurer.class, () -> configurer);
}Apply @PackInject to a DAO implementation and to the injection point:
@PackInject
public class StudentDAO implements DAO {}
@Resource
@PackInject
private DAO daos;All these techniques provide precise control over which bean is injected in a Spring application.
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.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.
