在对象构造中自动装配 Spring 服务
有没有一种方法可以让 Spring 在运行时注入对对象构造的依赖项,而不是在解析 Spring XML 文件或包扫描时?
我有一些扩展线程并进行文件传输的对象。很高兴能够自动装配这些 Thread 子类所依赖的服务,这样我就不必将它们全部传递到构造函数中。
额外问题!
一旦这些 Thread 子类完成 run() 实现,我需要对它们做些什么吗?他们需要以某种方式“杀戮”吗?
Is there a way I can have Spring inject dependencies on construction of an object at runtime, rather than when parsing the Spring XML files or on package-scan?
I've got some objects that extend Thread and do file transfers. It be nice to have the services these Thread sublasses depend on autowired, so I don't have to pass them all in the constructor.
BONUS QUESTION!
Is there anything I need to do with these Thread subclasses once they've done their run() implementation has finished? Do they need 'killing' somehow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会按原样使用 Spring 注入设施。您可以注入 ThreadPoolTaskExecutor 并让您的对象使用它。您的应用程序不会杀死或损害任何线程:Spring 将管理所有线程。
I'd use the Spring injection facilities as is. You can inject a ThreadPoolTaskExecutor and let your objects use that. No Threads will be killed or harmed by your application: Spring will manage them all.