【发布时间】:2018-08-18 17:44:49
【问题描述】:
我正在使用Spring Boot 并尝试在其中实现工厂设计模式。问题是当创建QuarterLevelStudentInternship 和QuarterLevelMou 的对象时,这些类中声明的自动装配依赖项设置为null,所以它抛出了NullPointerException。
表单操作
我创建了一个界面。
public interface FormOperation {
public Map<String, Object> fetchLevelsInfo(long levelId);
public Object fetchCurrentTargetLevel(long levelId);
}
QuarterLevelStudentInternship
该类实现FormOperation接口
@Component
public class QuarterLevelStudentInternship implements FormOperation {
@Autowired /*@Lazy*/
StudentInternshipService internshipService;
@Autowired /*@Lazy*/
StudentInternshipRecordService internshipRecordService;
// these two objects are showing null at the time of object generation and cause null pointer exception
@Override
public Map<String, Object> fetchLevelsInfo(long levelId) {
HashMap<String, Object> levels = new HashMap<>();
levels.put("internshipDetails", internshipService.fetchStudentInternship(levelId));
List<Map<String, Object>> internshipRecord = internshipRecordService.fetchStudentInternshipRecords(levelId);
levels.put("internshipRecord", internshipRecord);
levels.put("internshipGraph", internshipRecordService.fetchInternshipRecordsGroupbyOrganization(levelId));
levels.put("currentTargetLevel", internshipRecord.size());
return levels;
}
@Override
public Object fetchCurrentTargetLevel(long levelId) {
List<Map<String, Object>> internshipRecord = internshipRecordService.fetchStudentInternshipRecords(levelId);
return internshipRecord.size();
}
}
QuarterLevelMou
该类实现FormOperation接口
@Component
public class QuarterLevelMou implements FormOperation {
@Autowired /*@Lazy*/
MouServices mouService;
@Override
public Map<String, Object> fetchLevelsInfo(long levelId) {
HashMap<String, Object> levels = new HashMap<>();
List<Map<String, Object>> mouRecord = mouService.fetchMouResult(levelId);
levels.put("mouDetails", mouRecord);
levels.put("currentTargetLevel", mouRecord.size());
return levels;
}
@Override
public Object fetchCurrentTargetLevel(long levelId) {
List<Map<String, Object>> mouRecord = mouService.fetchMouResult(levelId);
return mouRecord.size();
}
}
FormOperationFactory
>它是一个基于evidanceForm生成对象的工厂类
@Component
public class FormOperationFactory {
public FormOperation createFormOperation(String evidanceForm) {
if (evidanceForm.equals("Student Internship Form"))
return new QuarterLevelStudentInternship();
else if (evidanceForm.equals("MOUS"))
return new QuarterLevelMou();
return null;
}
}
QuarterLevelOperations
这是我的服务类
@Service("quarterLevelOperations")
@Transactional
public class QuarterLevelOperations {
@Autowired @Lazy
QuarterLevelResultService resultService;
public List<Map<String, Object>> fetchLevelsInfoForForms(
List<Map<String, Object>> quarterLevels, String evidanceForm,
String year, boolean direction, Long quarterId) {
FormOperationFactory formOperationFactory = new FormOperationFactory();
for(Map<String, Object> levels :quarterLevels) {
//quarterLevels.forEach(levels -> {
long levelId = Long.parseLong(levels.get("id").toString());
if (evidanceForm == null) {
levels.put("evidance", resultService.fetchQuaterLevelResultEvidance(levelId));
}
else if (evidanceForm.equals("Student Internship Form")) {
FormOperation operation = formOperationFactory.createFormOperation(evidanceForm);
levels.putAll(operation.fetchLevelsInfo(levelId));
}
else if (evidanceForm.equals("MOUS")) {
FormOperation operation = formOperationFactory.createFormOperation(evidanceForm);
levels.putAll(operation.fetchLevelsInfo(levelId));
}
} //);
return quarterLevels;
}
}
【问题讨论】:
标签: java spring spring-boot factory autowired