Java 在计时器类对象中使用主方法中的空自动连线对象
我正在运行springboot应用程序,TimerTask的服务对象显示为空 我尝试了各种方法,但无法摆脱空指针异常 主课Java 在计时器类对象中使用主方法中的空自动连线对象,java,spring-boot,timertask,Java,Spring Boot,Timertask,我正在运行springboot应用程序,TimerTask的服务对象显示为空 我尝试了各种方法,但无法摆脱空指针异常 主课 @SpringBootApplication @ComponentScan(basePackages = {"com.comments.demo"}) public class NotifyMain { @Autowired static NotifyService notifyService; public static void
@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);
}
}
package com.comments.demo;
导入java.util.ArrayList;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Component;
@组成部分
公共类列表通知{
私有列表通知=新建ArrayList();
@自动连线
私人通知观察员通知观察员;
公共无效设置通知(列表通知){
this.notifications=通知;
}
公共作废添加(通知){
通知。添加(通知);
notifyListeners(通知);
}
私有void notifyListeners(通知newValue){
通知观察者观察(新值);
}
}
首先,我将listElement对象设为null。因此,我得到了这样一个结论:我应该使用注入的bean,而不是在schedule方法的参数中使用new NotifyService(),但我不知道如何做到这一点。在Spring中,您无法自动关联或手动关联静态字段。相反,请尝试setter注入:
private static NotifyService notifyService;
@Autowired
public void setNotifyService(NotifyService notifyService){
NotifyMain.notifyService= notifyService;
}
private static NotifyService notifyService;
@Autowired
public NotifyMain(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 void setNotifyService(NotifyService notifyService){
NotifyMain.notifyService= notifyService;
}
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);
}
主类内的run方法是应用程序的起点。我认为在启动应用程序之前,不能自动连接对象。试着这样做
@SpringBootApplication
public class NotifyMain {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(NotifyMain.class, args);
NotifyService notifyService = (NotifyService) context.getBean(NotifyService.class);
context.getBean(Timer.class).schedule(notifyService, 10, 10);
}
}
我认为ListNotification对象为空。这是接口还是服务?你能添加ListNotification代码吗?ListNotification是一个组件。在第四种方法中,我没有使用覆盖注释,而是使用了没有参数的PostConstruct注释,现在它可以工作了。