
线程类注入问题:无@autowired注入方法
你在编写线程类时遇到无法注入 redis 或接口的问题。这是因为线程无法使用@autowired进行注入。以下是如何使用构造注入方法解决此问题:
线程类:
@component
public class mythread extends thread {
private redistemplate redistemplate;
private myinterface myinterface;
public mythread(redistemplate redistemplate, myinterface myinterface) {
this.redistemplate = redistemplate;
this.myinterface = myinterface;
}
...
} 调用:
// 注入线程类 @autowired private mythread mythread; // 启动线程 mythread.start();
注入:
使用以下配置在主应用程序上下文中配置这三个 bean:
这样,redistemplate 和 myinterface 将通过构造函数注入 mythread 类,解决注入问题。










