在Timer类对象中使用它时,Main方法中的NULL自动装配对象 - java

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

我尝试了各种方法,但无法摆脱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和Notification类工作正常。

安慰

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。所以我得到了这一点,而不是在调度方法的参数中使用新的NotifyService(),我应该使用注入的bean,但是我不知道该怎么做。

参考方案

您不能在Spring中自动连线或手动连线静态字段。相反,请尝试使用setter注入:

private static NotifyService notifyService;

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

但是,仍然不能保证在使用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;
}

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

private static NotifyService notifyService;

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

要知道自动布线到静电场是不可取的。一个人不应该这样做。

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

@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:字节码和二进制有什么区别? - java

java字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…

Java-如何将此字符串转换为日期? - java

我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…

Java:从类中查找项目名称 - java

仅通过类的实例,如何使用Java反射或类似方法查找项目名称?如果不是,项目名称(我真正想要的是)可以找到程序包名称吗? 参考方案 项目只是IDE使用的简单组织工具,因此项目名称不是类或JVM中包含的信息。要获取软件包,请使用Class#getPackage()。然后,可以调用Package#getName()将包作为您在代码的包声明中看到的String来获取…