我需要在一个参数动态传入的方法中实例化一个bean(EmployeeSaver)。我不能使用构造函数设置器,因为这些值在配置时没有填充。
示例代码:
class MyEmployeeBean{
public void saveEmployeeDetail (Employee employee , EmployeeHistory hist ){
EmployeeDetail detail = hist.getDetail();
EmployeeSaver eSave = new EmployeeSaver(employee, detail)
saver.saveEmployee();
}
}
class EmployeeSaver {
private Employee empl;
private EmployeeDetail detail;
public EmployeeSaver(Employee emp, EmployeeDetail det){
empl = emp;
detail = det;
}
public void saveEmployee(){
// code to same the guy...
}
}
由于 MyEmployeeSaver 类没有默认构造函数,因此它会引发运行时异常。我无法使用以下配置,因为在我执行 hist.getDetail() 之前不知道employeeDetail!
<bean id="mySaverBean" class="come.saver.EmployeeSaver">
<constructor-arg name="empl" ref="employee" />
<constructor-arg name="hist" ref = "employeeHistory" />
</bean>
如何使用构造函数参数实例化employeeSaverBean?