弹簧@Autowired在新螺纹上不起作用
当我运行 TaskJob 时,我收到空指针异常,因为 Spring 没有自动布线服务Job服务。新线程导致此问题是因为Spring自动连接mysqlService没有问题吗?
public class TaskJob implements Runnable {
@Autowired
private ServiceJob serviceJob;
String name;
String source;
public TaskJob(String name, String source) {
this.name = name;
this.source = source;
}
public void run() {
serviceJob.run();
}
}
@Service
public class ServiceJob extends BaseJob{
@Autowired
private MysqlService mysqlService;
public void run(){
....
}
}
@Service
public class MysqlService {
...
}
我的应用程序上下文.xml;
<context:component-scan base-package="cm.*" />
我的课程是;
cm.tasks.jobs.TaskJob
cm.jobs.ServiceJob
cm.services.MysqlService;
编辑:TaskJob instanciated with;
TaskJob taskJob = new TaskJob(name, source);
Thread taskThread = new Thread(taskJob);
taskThread.start();