带线程的Spring-Mvc

带线程的Spring-Mvc,spring,spring-mvc,Spring,Spring Mvc,嗨,我的线程类显示空指针异常,请帮助我解决 @Component public class AlertsToProfile extends Thread { public final Map<Integer, List<String>> userMessages = Collections.synchronizedMap(new HashMap<Integer, List<String>>()); @Autowired

嗨,我的线程类显示空指针异常,请帮助我解决

@Component
public class AlertsToProfile extends Thread {

    public  final Map<Integer, List<String>> userMessages = Collections.synchronizedMap(new HashMap<Integer, List<String>>());

    @Autowired
    ProfileDAO profileDAO;

    private String categoryType;

    private String dataMessage;

    public String getCategoryType() {
        return categoryType;
    }

    public void setCategoryType(String categoryType) {
        this.categoryType = categoryType;
    }

    public String getDataMessage() {
        return dataMessage;
    }

    public void setDataMessage(String dataMessage) {
        this.dataMessage = dataMessage;
    }

    public void run() {

                String category=getCategoryType();
                String data= getDataMessage();
                List<Profile> all = profileDAO.findAll();
                if (all != null) {
                    if (category == "All" || category.equalsIgnoreCase("All")) {
                        for (Profile profile : all) {
                            List<String> list = userMessages.get(profile.getId());
                            if (list == null ) {
                                ArrayList<String> strings = new ArrayList<String>();
                                strings.add(data);
                                userMessages.put(profile.getId(), strings);
                            } else {
                                list.add(data);
                            }
                        }
                    } 
                }
             }

但是当我调用start()方法时,我得到了空指针异常,请帮助我。我不熟悉spring的线程概念,这很明显:您直接实例化线程,而不是让spring创建AlertStopProfile并自动连接实例

要解决此问题,请围绕run()方法创建一个Runnable并将其嵌入到方法中,如下所示:

public void startThread() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            // your code in here

        }}).start();
}

您需要将线程实例绑定到AlertsToProfile中的字段,以避免泄漏,并在完成后停止线程。

请发布完整的堆栈跟踪您直接实例化线程-
@Autowired AlertsToProfile AlertsToProfile
public void startThread() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            // your code in here

        }}).start();
}