java - 在 Timer 类对象中使用 Main 方法中的 NULL Autowired 对象

标签 java spring-boot timertask

我正在使用 TimerTask 运行 springboot 应用程序,我的服务对象显示为 null。

我尝试了各种方法,但无法摆脱空指针异常。

主类。

@SpringBootApplication
@ComponentScan(basePackages = {"com.comments.demo"}) 
public class NotifyMain {

    @Autowired
    static
    NotifyService notifyService;


    public static void main(String[] args) {

        Timer timer1 = new Timer();
        timer1.schedule(notifyService, 10, 10);
        SpringApplication.run(NotifyMain.class, args);

    }
}

服务等级

package com.comments.demo;
@Service
@Configurable
public class NotifyService extends TimerTask{

    @Autowired  
    ListNotification listElement;
        @Override
    public void run() {
    Notification notification= new Notification();
        listElement.add(notification);
    }

ListNotification 和通知类工作正常。

控制台

Exception in thread "main" java.lang.NullPointerException
at java.util.Timer.sched(Timer.java:399)
at java.util.Timer.schedule(Timer.java:248)
at com.comments.demo.NotifyMain.main(NotifyMain.java:22)

这是ListNotification的代码

 package com.comments.demo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ListNotification {

    private List<Notification> notifications = new ArrayList<>();

     @Autowired
     private NotificationObserver notificationObserver;

    public void setNotifications(List<Notification> notifications) {
        this.notifications = notifications;
    }

    public void add(Notification notification) {
        notifications.add(notification);
        notifyListeners(notification); 
    } 

    private void notifyListeners(Notification newValue) {
        notificationObserver.observation(newValue);
    }
}

首先我得到了 listElement 对象 null。所以我知道,我应该使用注入(inject)的 bean,而不是在调度方法的参数中使用新的 NotifyService(),但我不知道该怎么做。

最佳答案

您无法在 Spring 中 Autowiring 或手动装配静态字段。相反,尝试 setter 注入(inject):

private static NotifyService notifyService;

@Autowired
public void setNotifyService(NotifyService notifyService){
    NotifyMain.notifyService= notifyService;
}

但是,如果在使用之前注入(inject) NotifyService,则仍然无法保证。您也可以尝试第二种方法:

private static NotifyService notifyService;

@Autowired
private NotifyService autowiredNotifyService; //same as above but non-static this time. And you autowire this one.

@PostConstruct
private void init() {
   NotifyMain.notifyService = this.autowiredNotifyService;
}

第三种方法 -> 使用构造函数注入(inject):

private static NotifyService notifyService;

@Autowired
public NotifyMain(NotifyService notifyService){
    NotifyMain.notifyService= notifyService;
}

要知道 Autowiring 到静态字段是不可取的。人们不应该这样做。

由于您的应用程序更像是基于控制台的应用程序,因此也可以采用这种方法:

@SpringBootApplication
public class NotifyMain implements CommandLineRunner {

@Autowired
private NotifyService notifyService;

public static void main(String[] args) {
    SpringApplication.run(NotifyMain.class, args);
}

@Override
public void run(String... args) {
    Timer timer1 = new Timer();
    timer1.schedule(notifyService, 10, 10);
}

关于java - 在 Timer 类对象中使用 Main 方法中的 NULL Autowired 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56554873/

相关文章:

java - 局部变量可能尚未初始化 - Android 项目

spring - Spring @Import 中的 Kotlin 多个类

java - Tomcat 启动时间太长 - Java SecureRandom

java - TimerTask只触发一次

java - Apache POI 3.9 : WorkbookFactory method not found

java - 如何从 NetBeans 运行 JUnit?

java - 无法从 bootstrap.yml 读取配置服务器 uri

android - 如何取消定时器并更新同一个定时器?

java - 异步负载生成器

java - Hibernate/JPA 如何修复子类错误生成数据库表