
线程类注入问题:无@autowired注入方法
你在编写线程类时遇到无法注入 或接口的问题。这是因为线程无法使用@autowired进行注入。以下是如何使用构造注入方法解决此问题:
线程类:
@component
public class mythread extends thread {
private redistemplate<string, string> redistemplate;
private myinterface myinterface;
public mythread(redistemplate<string, string> redistemplate, myinterface myinterface) {
this.redistemplate = redistemplate;
this.myinterface = myinterface;
}
...
}
登录后复制
调用:
// 注入线程类 @autowired private mythread mythread; // 启动线程 mythread.start();
登录后复制
注入:
使用以下配置在主应用程序上下文中配置这三个 bean:
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" /> <bean id="myInterface" class="com.example.MyInterfaceImpl" /> <bean id="myThread" class="com.example.MyThread" />
登录后复制
这样,redistemplate 和 myinterface 将通过构造函数注入 mythread 类,解决注入问题。
以上就是线程类无法注入Redis或接口?如何使用构造注入解决?的详细内容,更多请关注IDCBABY其它相关文章!
