【发布时间】:2015-12-09 12:39:17
【问题描述】:
我想解决以下问题的最佳方法是什么
我有一个具有一些依赖项的服务和一个我需要的非 bean 字段
@Service
public class ServiceImpl{
@Autowired
OtherService otherService;
@Autowired
DaoImpl dao;
Entity entity;
}
我需要使用dao.getDefault() 方法初始化“实体”字段,这是我的方法
@Service
public class ServiceImpl{
@Autowired
OtherService otherService;
DaoImpl dao;
Entity entity;
@Autowired
public ServiceImpl(DaoImpl dao){
this.dao = dao;
entity = dao.getDefault();
}
}
混合基于构造函数和基于字段的依赖注入是一种好习惯吗?我无法使用@PostConstruct,因为我无权访问 spring 配置文件来启用它。感谢所有建议。
【问题讨论】:
标签: java spring dependencies autowired