NULL Autowired objeto en el método principal mientras que la usa en el objeto de la clase Timer

Akshay Busa:

Estoy funcionando con la aplicación springboot TimerTask mi objeto del servicio está mostrando nula.

He intentado varios métodos, pero incapaz de deshacerse de la excepción de puntero nulo.

clase principal .

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

    }
}

La clase de servicio

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 y clase de notificación están trabajando bien.

consola

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)

aquí está el código de 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);
    }
}

primero me estaba nula objeto listElement. así que tiene que en lugar de utilizar la nueva NotifyService () en el parámetro del método de programación que debería utilizar el bean inyectada pero la forma de hacerlo no sé.

faraz:

No se puede Autowire o campos estáticos alambre de forma manual en la primavera. En su lugar, trate de inyección de setter:

private static NotifyService notifyService;

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

Pero aún así, no habrá ninguna garantía si NotifyService se inyecta antes de utilizar. También puede probar segundo enfoque:

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;
}

Enfoque tercera -> Uso inyección de constructor:

private static NotifyService notifyService;

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

No sabe que Autowiring al campo estático no es deseable. Uno no debería hacerlo.

Debido a que su aplicación es más como una aplicación basada en consola, este enfoque también se puede tomar:

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

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=223698&siteId=1
Recomendado
Clasificación