Backend Development 9 min read

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.

<code>@Autowired
private UserService userService;
</code>

2. Collections and sub‑interfaces

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

<code>@Autowired
private List<MessageNotifier> messageNotifiers;
</code>

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

3. Arrays

@Autowired can also inject an array of beans.

<code>@Autowired
private MessageNotifier[] messageNotifiers;
</code>

4. Maps

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

<code>@Autowired
private Map<String, MessageNotifier> messageNotifierMap;
</code>

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.

<code>@Autowired
@Lazy
private MessageNotifier messageNotifier;
</code>

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.

<code>@Autowired
private Optional<MessageNotifier> messageNotifier;
</code>

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.

<code>@Autowired
private ObjectFactory<MessageNotifier> messageNotifierObjectFactory;

@Autowired
private ObjectProvider<MessageNotifier> messageNotifierObjectProvider;
</code>

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.

<code>&lt;dependency&gt;
    &lt;groupId&gt;javax.inject&lt;/groupId&gt;
    &lt;artifactId&gt;javax.inject&lt;/artifactId&gt;
    &lt;version&gt;1&lt;/version&gt;
&lt;/dependency&gt;
</code>

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.

Javabackend-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

login 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.