首先需要知道的是,容器中的组件,也就是你添加了诸如 @Component@Service@Controller 以及 @Repository 等等注解,在容器启动的时候是会扫描标注这些注解的类创建 Bean 并放入容器中。

如果该类中的成员变量上使用了诸如 @Autowired@Resource 注解时,容器将会找对应的 Bean 并注入,又叫依赖注入

而在多线程实例中使用 @Autowired 注解得不到对象,又叫 Null,为什么呢?

这是因为多线程是防注入的,所以只是在多线程实现类中简单的使用 @Autowired 方法注入自己的 service,会在程序运行到此类调用 service 方法的时候提示注入的 service 为 null。

 

 

解决方法:直接将实例化的实体类传入线程类

package com.lvjing.thread;

import com.lvjing.common.MsgHandler;
import com.lvjing.service.DeviceDataService;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

//@Component
public class MyThread implements Runnable {
//    @Autowired
//    private static MsgHandler msgHandler;

    private static MyThread myThread;

//    @PostConstruct
//    public void init(){
//        myThread = this;
//        myThread.msgHandler = this.msgHandler;
//    }

    private String topic;
    private MqttMessage mqttMessage;
    private MsgHandler msgHandler;
    public MyThread(MsgHandler msgHandler,String topic,MqttMessage mqttMessage){
        this.msgHandler = msgHandler;
        this.topic = topic;
        this.mqttMessage = mqttMessage;
    }

    @Override
    public void run(){
        try {
            System.out.println(Thread.currentThread().getName());
            System.out.println(myThread);
            System.out.println(msgHandler);
            msgHandler.msgHandle(this.topic,this.mqttMessage);
    }catch (Exception e){
        e.printStackTrace();
        }
    }
}
public MyThread(MsgHandler msgHandler,String topic,MqttMessage mqttMessage){
        this.msgHandler = msgHandler;
        this.topic = topic;
        this.mqttMessage = mqttMessage;
    }
在这里实例化线程类的时候传入对应的工具类。


实际应用场景:
@Autowired
MsgHandler msgHandler;
// 设置回调函数
            client.setCallback(new MqttCallback() {
                public void connectionLost(Throwable cause) {
                    System.out.println("connectionLost");
                }
                public void messageArrived(String topic, MqttMessage message) throws Exception {
                    System.out.println(message);
                    //若收到信息就走处理信息的方法
//                    msgHandlerService.msgHandle(topic,message);
//                    MsgHandler msgHandler = new MsgHandler();
//                    factory.autowireBean(msgHandler);

                    MyThread task = new MyThread(msgHandler,topic,message);
                    executor.execute(task);
//                    executor.remove(task);
                }
                public void deliveryComplete(IMqttDeliveryToken token) {
                    System.out.println("deliveryComplete---------"+ token.isComplete());
                }
            });

 工具类:

用@service注解