带有@Autowired存储库的SpringBootApp NullPointerException

SpringBootApp NullPointerException with @Autowired repository

这是我的 Spring 启动应用程序。 当我 运行 main 方法总是抛出一个空指针异常。 我不知道为什么 @Autowired JsonReaderService 为空。正如我将其定义为组件。

它是项目src文件夹中的一个子文件夹,所以Main Method在源文件夹上面。所以spring应该扫描正确吧??

我还有一个测试方法,效果很好。

 @SpringBootApplication
 public class DemoApplication {

@Autowired
private JsonReaderService jsonReaderService;

private static JsonReaderService stat_jsonReaderService;

static Logger logger = LoggerFactory.getLogger(DemoApplication.class);

public static void main(String[] args) throws IOException {

    String textFileName = scanFileName();
    Reader reader = Files.newBufferedReader(Paths.get("src/main/resources/" + textFileName));
    // This line is always null pointer exception. The @autowired Annotation don't work with my JsonReaderService????? WHY
    List<EventDataJson> jsonReaderServicesList = stat_jsonReaderService.readEventDataFromJson(reader);
    stat_jsonReaderService.mappingToDatabase(jsonReaderServicesList);

}

public static String scanFileName() {
    logger.info("Scanning keyboard input");
    System.out.println("enter a file to scan");
    Scanner scanInput = new Scanner(System.in);
    String text = scanInput.nextLine();
    logger.info("successful keyboard input was : " + text);
    return text;
}

@PostConstruct
public void init() {
    logger.info("initializing Demo Application");
    stat_jsonReaderService = jsonReaderService;
}

}

这里我有 class,它使用存储库 @Autowired 来保存一些实体,但我总是在行 repository.save(...)

中得到空指针异常
@Component
public class JsonReaderService {
static Logger logger = LoggerFactory.getLogger(DemoApplication.class);
@Autowired
EventDataRepository repository;
private Reader reader;
private List<EventDataJson> eventDataList;

@Autowired
public JsonReaderService(){}

public List<EventDataJson> readEventDataFromJson(Reader reader) throws IOException {

    try {
        logger.info("parsing event data from json started");
        Gson gson = new Gson();
        EventDataJson[] eventData = gson.fromJson(reader, EventDataJson[].class);
        eventDataList = Arrays.asList(eventData);
        reader.close();
    } catch (IOException e) {
        logger.error("Error while reading the json file");
        e.printStackTrace();
    }
    logger.info("parsing json eventData successful finished");
    return eventDataList;
}

public Boolean mappingToDatabase(List<EventDataJson> eventDataList) {
    logger.info("mapping from json to database eventData started ...");

    Set<String> idList = eventDataList.stream().map(EventDataJson::getId).collect(Collectors.toSet());
    for (String id : idList
    ) {
        Stream<EventDataJson> filteredEventDataList1 = eventDataList.stream().filter((item) -> item.getId().equals(id));
        Stream<EventDataJson> filteredEventDataList0 = eventDataList.stream().filter((item) -> item.getId().equals(id));
        EventDataJson startedEvent = filteredEventDataList1.filter((item) -> item.getState().equals("STARTED")).findAny().orElse(null);
        EventDataJson finishedEvent = filteredEventDataList0.filter((item) -> item.getState().equals("FINISHED")).findAny().orElse(null);
        long duration0 = finishedEvent.getTimestamp() - startedEvent.getTimestamp();
        Boolean alert;
        if (duration0 > 4) {
            alert = true;
        } else {
            alert = false;
        }
        try {
            this.repository.save(new EventDataDb(id, duration0, startedEvent.getType(), startedEvent.getHost(), alert));
            logger.info("mapping to Database Repository action successful");

        } catch (Exception e) {
            logger.error("Exception in database mapping occurred");
            e.printStackTrace();
            return false;

        }
    }
    return true;

}
}

带注释的存储库

@Repository
 public interface EventDataRepository extends JpaRepository<EventDataDb, String> {
 EventDataDb findAllById(String id);
 }

测试用例与@autowired 注释一起工作得很好我不知道为什么它在 main 方法中不起作用。是因为它是静态的吗?

    @Autowired
       private EventDataRepository repository;

    @Autowired
       private JsonReaderService jReader;
    @Test
    public void whenParseJson_thenTransform_and_save_to_db() throws IOException {
    BufferedReader reader = Files.newBufferedReader(Paths.get("src/main/resources/" + "logfile.txt"));
    List<EventDataJson> eventDataList1 = jReader.readEventDataFromJson(reader);
    if (jReader.mappingToDatabase(eventDataList1)) {
        EventDataDb eventDataFromDb = this.repository.findAllById("scsmbstgra");
        Assertions.assertTrue(eventDataFromDb.getType().equals("APPLICATION_LOG"));
        Assertions.assertTrue(eventDataFromDb.getHost().equals("12345"));
        Assertions.assertTrue(eventDataFromDb.getAlert().equals(true));
        Assertions.assertTrue(eventDataFromDb.getDuration() == 5);
        logger.info("Assert successfully accomplished");
    } else
        logger.error("Could not persist eventData to DB Error");
}

堆栈跟踪

`线程“restartedMain”中的异常java.lang.reflect.InvocationTargetException

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

原因:java.lang.NullPointerException 在 com.creditsuisse.demo.DemoApplication.main(DemoApplication.java:33) ... 还有 5 个`

您需要 运行 SpringApplication.run() 因为此方法会启动整个 Spring 框架。由于您的代码中没有它,因此 bean 不会自动装配并且 JsonReaderService 为 null。您可以在 Application.java 中执行以下操作。此外,由于这涉及从 CLI 获取输入,为什么不按如下方式使用 CommandLineRunner:

@SpringBootApplication
public class DemoApplication 
  implements CommandLineRunner {

    @Autowired
    private JsonReaderService jsonReaderService;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
 
    @Override
    public void run(String... args) {
        String textFileName = scanFileName();
        Reader reader = Files.newBufferedReader(Paths.get("src/main/resources/" + textFileName));
        List<EventDataJson> jsonReaderServicesList = stat_jsonReaderService.readEventDataFromJson(reader);
        jsonReaderService.mappingToDatabase(jsonReaderServicesList);
    }
}