我见过例如由Martin Fowler Here重构有条件的多态性弹簧引导代码
不知道我怎么能在春天启动的方式实现它(IOC)在重构给出。
我正在处理spring web应用程序。 我有一个REST控制器,它接受studentId
和fileType
和导出数据学生给定fileType
格式。 控制器调用ExportService
exportFile()
方法,它看起来像
@Service
public class ExportServiceImpl implements ExportService {
public void exportFile(Integer studentId, String fileType) {
if (XML.equals(fileType)) { exportXML(studentId);}
else if()... // and so on for CSV, JSON etc
}
}
重构条件多态,
首先,我创建抽象类,
abstract class ExportFile {
abstract public void doExport(Integer studentId);
}
然后我,每种文件类型导出创建服务。对于下面的示例XML导出是一种服务,
@Service
public class ExportXMLFileService extends ExportFile {
public void doExport(Integer studentId) {
// exportLogic will create xml
}
}
现在我ExportService应该是什么样子,
@Service
public class ExportServiceImpl implements ExportService {
@Autowired
private ExportFile exportFile;
public void exportFile(Integer studentId, String fileType) {
exportFile.doExport(studentId);
}
}
现在我在这里停留:(
无法获取, 如何@Autowired
ExportFile
将根据fileType
知道具体哪项服务?
请做正确我如果我错了。您的回应将不胜感激:)