方法结束后丢失已解决的实例/null (@Autowire)

Loosing setted instance / null after method ending (@Autowire)

我正在尝试根据 URI 设置解析器。 我正在调试它。 当条件正常时,我的 parserParent 已设置,但在 setParser() 方法结束时,parserParent 再次为空。我试图在继承 class 中结合使用 @Autowired 注释,但我总是遇到相同的 NullPointer 错误。 如何解决?

CLASS 问题出在哪里

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";
    }
}

PARENT CLASS 解析器

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 {}

}

CHILD CLASS

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);

    }

问题已解决! :D

代码如下:

@RestController
public class RestClientSave {

    ParseCsvAndSaveToDB parseCsvAndSaveToDB;
    ParseXlsxAndSaveToDb parseXlsxAndSaveToDb;

    @Autowired
    public RestClientSave(ParseCsvAndSaveToDB parseCsvAndSaveToDB, ParseXlsxAndSaveToDb parseXlsxAndSaveToDb) {
        this.parseCsvAndSaveToDB = parseCsvAndSaveToDB;
        this.parseXlsxAndSaveToDb = parseXlsxAndSaveToDb;
    }

    public ParserParent setParser(ParseCsvAndSaveToDB parseCsvAndSaveToDB,
                                  ParseXlsxAndSaveToDb parseXlsxAndSaveToDb,
                                  String whichParser) {
        ParserParent parserParent = null;

        if (whichParser.equals("csv")) {
             return parserParent = parseCsvAndSaveToDB;
        }else if(whichParser.equals("xlsx")) {
             return parserParent = parseXlsxAndSaveToDb;
        }

        return parserParent;
    }

    @GetMapping("/save/{whichParser}")
    public String save(@PathVariable String whichParser) throws IOException {

        ParserParent parent = setParser( parseCsvAndSaveToDB, parseXlsxAndSaveToDb, whichParser);
        parent.save();

        return "data saved";
    }
}