环境:Spring5.3.23
1. Optional
该类是java1.8提供,java.util.Optional。
static class CommonDAO {
}
static class CommonService {
@Resource
private Optional 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)) ;
}
输出
CommonService [optional=xxx.CommonDAO@12d4bf7e]
2. ObjectFactory
该类Spring提供,org.springframework.beans.factory.ObjectProvider
static class CommonService {
@Resource
private ObjectProvider provider ;
@Override
public String toString() {
return "CommonService [provider=" + provider.getIfAvailable() + "]";
}
}
输出
CommonService [provider=xxx.CommonDAO@11a9e7c8]
3. Provider
static class CommonService {
@Resource
private javax.inject.Provider provider ;
@Override
public String toString() {
return "CommonService [provider=" + provider.get() + "]";
}
}
输出
CommonService [provider=xxx.CommonDAO@2f177a4b]
注意:如果你的环境中没有javax.inject.Provider,你需要引入以下包:
javax.inject
javax.inject
1
4. 数组
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) + "]";
}
}
输出
CommonService [daos=[xxx.DogDAO@4445629, xxx.CatDAO@45b9a632]]
5. Collection集合
static class CommonService {
@Resource
private List daos ;
@Override
public String toString() {
return "CommonService [daos=" + daos + "]";
}
}
输出
CommonService [daos=[xxx.DogDAO@309e345f, xxx.CatDAO@56a6d5a6]]
6. Map集合
static class CommonService {
@Resource
private Map daos ;
@Override
public String toString() {
return "CommonService [daos=" + daos + "]";
}
}
输出
CommonService [daos={dataTypeInejctMain.DogDAO=xxx.DogDAO@4445629, dataTypeInejctMain.CatDAO=xxx.CatDAO@45b9a632}]
7. 特殊说明
7.1 使用@Value注解
1. 注入普通字面量
static class CommonService {
@Value("${pack.name}")
private String name ;
@Override
public String toString() {
return "CommonService [name=" + name + "]";
}
}
// 注备环境
pack.name=中国🇨🇳
输出
CommonService [name=中国🇨🇳]
2. 根据SpEL表达式注入
static class CommonService {
@Value("#{${pack.name}}")
private CommonDAO dao ;
@Override
public String toString() {
return "CommonService [name=" + dao + "]";
}
}
// 注备环境, 这里是配置的其它bean的名称
pack.name=commonDao
// 注册Bean
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
// ...
context.registerBean("commonDao", CommonDAO.class) ;
context.register(CommonService.class) ;
// ...
System.out.println(context.getBean(CommonService.class)) ;
}
输出
CommonService [name=xxx.CommonDAO@29176cc1]
3. 注入基本数据
static class CommonService {
// 会自动将值转换为Integer
// 只要Spring的类型转换器支持的都可以通过下面方式注入
@Value("666")
private int id ;
@Override
public String toString() {
return "CommonService [id=" + id + "]";
}
}
输出
CommonService [id=666]
7.2 使用@Lazy注解
使用@Lazy注解后,注入的将会是一个代理对象,只有真正使用到的时候才会去真实的获取具体的bean。
static class CommonService {
@Resource
@Lazy
private CommonDAO commonDao ;
@Override
public String toString() {
return "CommonService [commonDao=" + commonDao.getClass() + "]";
}
}
输出
CommonService [commonDao=class xxx.CommonDAO$$EnhancerBySpringCGLIB$$39f36385]
以上是本篇文章的全部内容,希望对你有所帮助。
完毕!!!