我正在开发一个 spring mvc,我希望我的 Controller 能够监听应用程序事件 通过@controller我从网页发送一条jms消息 我试图在收到消息 jms 时通知 Controller ,以便将一些数据推送到网页
首先,我尝试使用观察者模式,将 Controller 实现为 ApplicationListener
@ Controller ("MyController")
@ Scope (value = "session" = proxyMode ScopedProxyMode.TARGET_CLASS)
public class MyController implements Serializable, ApplicationListener <CustomEvent>
然后我尝试在收到 jms 消息时从另一个 bean 调用 Controller 方法
@Resource(name="MyController")
private MyController c;
c.update(String data);
但我总是遇到同样的错误
No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
有没有办法从另一个 bean 调用 Controller ? 谢谢
最佳答案
问题在于您的 Controller 是 session 范围的,并且您尝试从没有可用 HTTP session 的上下文中调用其方法。
因此,我假设您的 Controller 结合了依赖于 HTTP session 的功能(即 session
范围)和需要在 session 外部调用的功能(例如 update()
方法)。
如果是这样,您可以通过将不需要 HTTP session 的功能移至单独的 bean(没有 session
范围)来解决此问题。
关于java - 从另一个 bean 中调用 Controller 。未找到线程绑定(bind)请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24363486/