我有一个实体,例如 Employee,其中包含 @Transient 对象薪资,该薪资将从相关表/实体 DailyTimeRecord (DTR) 派生。DTR 对象数据检索使用连接,并且也在 Employee 对象中自动装配。DTR 对象列表将作为计算薪资对象值的基础。
new
应避免使用using关键字,并让IoC容器创建对象。另外,我希望避免使用new
关键字,以尽量减少代码的耦合性,并尽可能确保未来的兼容性和支持可扩展性。因此,我有接口 Salary 并由 SalaryImpl 类实现。
但是每次我尝试在瞬态属性 Salary 上运行自动装配的代码时,它总是为空。我在这里找到了根本原因
new
当它是瞬态属性时,我将如何创建一个避免使用关键字的对象?
实体类
@Entity
Class Employee implements Serializable {
//Attributes from DB here
@OneToMany
@JoinColumn(name="empNumber", referencedColumnName = "empNumber")
private List<DTR> dtr;
@Autowired
@Transient
private Salary salary;
//getters ang setters here
public double computeSalary(){
}
}
薪资接口
public interface Salary {
public double computeSalary(List<Benefit> benefits, List<Deduction> deductions);
}
薪资接口基础/实现类
@Service
public class SalaryImpl implements Salary, Serializable {
//other attributes here
//getter and setters
//other methods
@Override
public double computeSalary(List<Benefit> benefits, List<Deduction> deductions){
return 0;
}
}
梵蒂冈之花
泛舟湖上清波郎朗
相关分类