Spring Dependency Injection Tricks: Optional, Provider, Collections, @Value & @Lazy
This article demonstrates various Spring dependency‑injection techniques—including Optional, ObjectProvider, Provider, arrays, collections, maps, @Value literals and SpEL expressions, as well as @Lazy proxies—showing the code and resulting bean representations for each approach.
Environment: Spring 5.3.23 (Java 1.8).
1. Optional
java.util.Optional<T> can be injected to lazily obtain a bean.
static class CommonDAO {}
static class CommonService {
@Resource
private Optional<CommonDAO> optional;
@Override
public String toString() {
return "CommonService [optional=" + optional.orElseGet(() -> null) + "]";
}
}
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CommonDAO.class, CommonService.class)) {
System.out.println(context.getBean(CommonService.class));
}Output:
CommonService [optional=xxx.CommonDAO@12d4bf7e]2. ObjectFactory (ObjectProvider)
Spring’s ObjectProvider can be used to obtain a bean when needed.
static class CommonService {
@Resource
private ObjectProvider<CommonDAO> provider;
@Override
public String toString() {
return "CommonService [provider=" + provider.getIfAvailable() + "]";
}
}Output:
CommonService [provider=xxx.CommonDAO@11a9e7c8]3. Provider (javax.inject.Provider)
javax.inject.Provider offers a standard way to retrieve a bean.
static class CommonService {
@Resource
private javax.inject.Provider<CommonDAO> provider;
@Override
public String toString() {
return "CommonService [provider=" + provider.get() + "]";
}
}Output: CommonService [provider=xxx.CommonDAO@2f177a4b] Note: If javax.inject.Provider is missing, add the following Maven dependency:
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>4. Array Injection
static interface DAO {}
static class DogDAO implements DAO {}
static class CatDAO implements DAO {}
static class CommonService {
@Resource
private DAO[] daos;
@Override
public String toString() {
return "CommonService [daos=" + Arrays.toString(this.daos) + "]";
}
}Output:
CommonService [daos=[xxx.DogDAO@4445629, xxx.CatDAO@45b9a632]]5. Collection Injection (List)
static class CommonService {
@Resource
private List<DAO> daos;
@Override
public String toString() {
return "CommonService [daos=" + daos + "]";
}
}Output:
CommonService [daos=[xxx.DogDAO@309e345f, xxx.CatDAO@56a6d5a6]]6. Map Injection
static class CommonService {
@Resource
private Map<String, DAO> daos;
@Override
public String toString() {
return "CommonService [daos=" + daos + "]";
}
}Output:
CommonService [daos={dataTypeInejctMain.DogDAO=xxx.DogDAO@4445629, dataTypeInejctMain.CatDAO=xxx.CatDAO@45b9a632}]7. Special Cases
7.1 Using @Value
Injecting literal values:
static class CommonService {
@Value("${pack.name}")
private String name;
@Override
public String toString() {
return "CommonService [name=" + name + "]";
}
}
// property file
pack.name=中国🇨🇳Output: CommonService [name=中国🇨🇳] Injecting via SpEL expression:
static class CommonService {
@Value("#{${pack.name}}")
private CommonDAO dao;
@Override
public String toString() {
return "CommonService [name=" + dao + "]";
}
}
// property
pack.name=commonDao
// register bean
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.registerBean("commonDao", CommonDAO.class);
context.register(CommonService.class);
System.out.println(context.getBean(CommonService.class));
}Output: CommonService [name=xxx.CommonDAO@29176cc1] Injecting basic data types (automatic conversion):
static class CommonService {
@Value("666")
private int id;
@Override
public String toString() {
return "CommonService [id=" + id + "]";
}
}Output:
CommonService [id=666]7.2 Using @Lazy
@Lazy creates a proxy; the actual bean is instantiated only when accessed.
static class CommonService {
@Resource
@Lazy
private CommonDAO commonDao;
@Override
public String toString() {
return "CommonService [commonDao=" + commonDao.getClass() + "]";
}
}Output:
CommonService [commonDao=class xxx.CommonDAO$$EnhancerBySpringCGLIB$$39f36385]These examples cover the most common ways to inject dependencies in Spring, helping developers choose the appropriate technique for their use case.
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.
