春天 + struts2,将DAO注入外线程
我有一个使用 Struts2 + Spring 进行资源注入的 Web 应用程序,基本上是我的 DAO。现在我想创建一个线程来定期轮询数据库,并在需要时向用户发送电子邮件通知。
我想知道如何以该线程可以使用我的 DAO 的方式实现这一点。我无法管理 Spring 以我的方式注入它。所以我想听听建议,看看是否有人能给我指出正确的方法。
现在我有一个由 ServletContextListener 启动的线程,它只创建一个计时器并每 5 分钟安排一个操作。但我无法通过此操作来使用我的 DAO。我不需要使用这种结构,我愿意使用任何可行的解决方案。
感谢您的帮助!
编辑:正如axtavt所建议的,我使用了Spring任务执行调度并且它工作得很好,问题是我的任务被注入了DAO,但是每次我尝试访问我的属性时我都会得到LazyInitializationException获取对象,关于如何解决这个问题有什么建议吗?
I have a web application that uses Struts2 + Spring for the resource injection, basically my DAO. Now I would like to create a thread that periodically polls the database and, if needed, send email notifications to users.
I would like to know how I can implement this in a way that this thread can use my DAO. I haven't been able to manage Spring to inject it the way I've done it. So I would like to hear suggestions and see if someone can point me to the right way.
Right now I have a thread started by a ServletContextListener, that just creates a timer and schedules an action every 5 minutes. But I can't get this action to use my DAO. I don't have any need to use this structure, I'm open to using whichever solution works.
Thanks for your help!
Edit: As axtavt suggested, I used Spring task Execution Scheduling and it works perfectly, the thing is that my task gets injected with the DAO but then I get LazyInitializationException every time I try to access a property of my fetched objects, any suggestion on how to solve that??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许最好的选择是使用 Spring 自己的调度支持,请参阅 25。任务执行和调度(如果需要 - 使用Quartz,请参阅25.6 使用 OpenSymphony Quartz Scheduler) 。该方法允许您将计划的操作配置为 Spring bean,以便您可以将它们与其他 bean(例如 DAO)连接。
或者,您可以使用以下命令获取 Web 应用程序中的任何 Spring bean(例如,从您的线程获取 DAO):
Perhaps the best option is to use Spring's own scheduling support, see 25. Task Execution and Scheduling (if necessary - with Quartz, see 25.6 Using the OpenSymphony Quartz Scheduler). This apporach allows you to configure your scheduled action as Spring beans, so you can wire them with other beans such as DAO.
Alternatively, you can use the following to obtain any Spring bean in web application (for example, to obtain DAO from your thread):