线程类注入问题:无@autowired注入方法
你在编写线程类时遇到无法注入 redis 或接口的问题。这是因为线程无法使用@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或接口?如何使用构造注入解决?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号