I am using Spring Boot
and trying to implement factory design pattern in it. The issue is that when the objects of QuarterLevelStudentInternship
and QuarterLevelMou
are created, the autowired dependencies declared inside those classes are set to null, so it throws me NullPointerException
.
FormOperation
I created an interface.
public interface FormOperation {
public Map<String, Object> fetchLevelsInfo(long levelId);
public Object fetchCurrentTargetLevel(long levelId);
}
QuarterLevelStudentInternship
This class implements FormOperation interface
@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
This class implements FormOperation interface
@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
It's a factory class which generate object based on 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
It's my service class
@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;
}
}