小编Aks*_*usa的帖子

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

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

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

主要类。

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

    }
}
Run Code Online (Sandbox Code Playgroud)

服务类

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);
    }
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

这是 ListNotification 的代码 …

java timertask spring-boot

2
推荐指数
1
解决办法
330
查看次数

标签 统计

java ×1

spring-boot ×1

timertask ×1