8 Ways Spring Can Autowire Beans: Collections, Maps, Lazy, Optional & More

This article explains eight bean types that Spring's @Autowired can inject—including single beans, collections, arrays, maps, lazy proxies, Optional, ObjectFactory/ObjectProvider, and JSR‑330 Provider—showing how each works and when to use them.

Sanyou's Java Diary
Sanyou's Java Diary
Sanyou's Java Diary
8 Ways Spring Can Autowire Beans: Collections, Maps, Lazy, Optional & More

The article discusses lesser‑known Spring dependency‑injection capabilities, covering eight bean types that can be autowired.

1. Single bean

Standard injection using @Autowired on a field, e.g., UserService.

@Autowired
private UserService userService;

2. Collections and sub‑interfaces

@Autowired can inject a Collection such as List<MessageNotifier> to receive all implementations of an interface.

@Autowired
private List<MessageNotifier> messageNotifiers;

Injection must be a Collection type; injecting a concrete ArrayList is not supported.

3. Arrays

@Autowired can also inject an array of beans.

@Autowired
private MessageNotifier[] messageNotifiers;

4. Maps

Injecting a Map<String, MessageNotifier> where the key is the bean name.

@Autowired
private Map<String, MessageNotifier> messageNotifierMap;

Only the Map interface is supported, not sub‑interfaces.

5. @Lazy

Adding @Lazy makes the injection lazy; Spring injects a proxy that creates the real bean only when first used.

@Autowired
@Lazy
private MessageNotifier messageNotifier;

This can resolve circular‑dependency problems such as those caused by @Async.

6. Optional

Java 8 Optional can be autowired to avoid NoSuchBeanDefinitionException when a bean is absent.

@Autowired
private Optional<MessageNotifier> messageNotifier;

Alternatively, set required=false on @Autowired.

7. ObjectFactory and ObjectProvider

Both are Spring interfaces that support lazy retrieval; ObjectProvider also offers safe retrieval via getIfAvailable.

@Autowired
private ObjectFactory<MessageNotifier> messageNotifierObjectFactory;

@Autowired
private ObjectProvider<MessageNotifier> messageNotifierObjectProvider;

ObjectFactory requires an explicit getObject() call; ObjectProvider can return null instead of throwing when the bean is missing.

8. JSR‑330 Provider

JSR‑330 defines javax.inject.Provider, which Spring can inject similarly to ObjectFactory for lazy injection.

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

Summary

Single bean injection.

Collection injection (arrays or collections).

Lazy injection (@Lazy, ObjectFactory, ObjectProvider, JSR‑330 Provider).

Safe injection (Optional, ObjectProvider).

These mechanisms are independent and can be combined, for example lazy injection of a collection.

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.

backend-developmentspringdependency-injectionAutowired
Sanyou's Java Diary
Written by

Sanyou's Java Diary

Passionate about technology, though not great at solving problems; eager to share, never tire of learning!

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.