gpt4 book ai didi

java - 方法结束后丢失已确定的实例/null (@Autowire)

转载 作者:行者123 更新时间:2023-12-01 18:13:35 25 4
gpt4 key购买 nike

我正在尝试根据 URI 设置解析器。我正在调试它。当条件确定时,我的 parserParent 已设置,但在 setParser() 方法结束时,parserParent 再次为 null。我试图在继承类中结合分配 @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";
}
}

解析器的父类

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 类

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

关于java - 方法结束后丢失已确定的实例/null (@Autowire),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60417875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com