Я пытаюсь установить парсер в зависимости от URI. Я отлаживал это. Когда условие в порядке, мой parserParent установлен, но в конце метода setParser () parserParent снова становится нулевым. Я пытался объединить с присваиванием аннотации @Autowired в наследуемом классе, но я всегда получаю ту же ошибку NullPointer. Как это исправить?
КЛАСС, ГДЕ ПРОБЛЕМА
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import pl.manciak.excelparser.ParseAndSave.ParseCsvAndSaveToDB;
import pl.manciak.excelparser.ParseAndSave.ParseXlsxAndSaveToDb;
import pl.manciak.excelparser.ParseAndSave.ParserParent;
import java.io.IOException;
@RestController
public class RestClientSave {
private ParserParent parserParent;
private ParseCsvAndSaveToDB parseCsvAndSaveToDB;
private ParseXlsxAndSaveToDb parseXlsxAndSaveToDb;
private String whichParser= "csv"; //HARDCODED FOR SIMPLICITY
@Autowired
public void setParser( ParseCsvAndSaveToDB parseCsvAndSaveToDB,
ParseXlsxAndSaveToDb parseXlsxAndSaveToDb) {
if (whichParser.equals("csv")) {
parserParent = parseCsvAndSaveToDB; // HERE PARSER IS SETTED
}else if(whichParser.equals("xlsx")) {
this.parserParent = parseXlsxAndSaveToDb;
}
}
@GetMapping("/save/{whichParser}")
public String save(@PathVariable String whichParser) throws IOException {
this.whichParser= whichParser;
setParser( parseCsvAndSaveToDB, parseXlsxAndSaveToDb); // HERE IS AGAIN NULL
parserParent.save();
return "data saved";
}
}
РОДИТЕЛЬСКИЙ КЛАСС ДЛЯ ПАРСЕРА
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pl.manciak.excelparser.DataService;
import pl.manciak.excelparser.Entity.LinesEntity;
import pl.manciak.excelparser.Entity.MapEntity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
@Service
public class ParserParent {
protected DataService dataService;
protected LinesEntity linesEntity;
protected ArrayList<String> list;
protected HashMap<Long, LinesEntity> xlsMapped = new HashMap<>();
protected MapEntity mapEntity = new MapEntity();
@Autowired
public ParserParent(DataService dataService ) {
this.dataService = dataService;
}
public void save() throws IOException {}
}
ДЕТСКИЙ КЛАСС
package pl.manciak.excelparser.ParseAndSave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pl.manciak.excelparser.DataService;
import pl.manciak.excelparser.Entity.LinesEntity;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
@Service
public class ParseCsvAndSaveToDB extends ParserParent{
@Autowired
public ParseCsvAndSaveToDB(DataService dataService) {
super(dataService);
}
public void save() throws IOException {
//Stream to Read Csv file
FileReader fileReader = new FileReader("usda_sample.csv");
BufferedReader br = new BufferedReader(fileReader);
//read first line
String line = br.readLine();
long mapKey = 0;
while (line != null) {
linesEntity = new LinesEntity(); // create a new LinesEntity for this loop execution
list = new ArrayList<>();
Collections.addAll(list, line.split(","));
line = br.readLine();
linesEntity.setSingleLine(new ArrayList<>(list));
dataService.saveOne(linesEntity);
xlsMapped.put(mapKey, linesEntity);
mapKey++;
}
mapEntity.setMapa(xlsMapped);
System.out.println(xlsMapped);
dataService.save(mapEntity);
}