如何在没有web.xml的情况下注册监听器

时间:2012-03-27 09:39:30

标签: spring jersey grizzly

我目前正在做一个灰熊,春季和球衣项目,我遇到过:

Caused by: java.lang.IllegalStateException: 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.
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
    at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:40)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:328)
    ... 32 more

基于堆栈跟踪(以及我google时的结果),我应该在web.xml中注册一个监听器

<web-app ...>
   <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
   </listener>
</web-app>

所以我的问题是,鉴于我没有web.xml,我将如何将监听器注册到灰熊服务器?

1 个答案:

答案 0 :(得分:0)

如果没有看到更多的代码,我的猜测是使用ServletHandler - 查看此类的Javadocs顶部的示例源代码:

普通的spring应用程序将为上下文启动注册一个侦听器,然后为大多数其他事件调度servlet,因此您应该能够修改示例代码来执行此操作。

像(完全未经测试)的东西:

sa.setServlet(new org.springframework.web.servlet.DispatcherServlet());
sa.addListener(org.springframework.web.context.ContextLoaderListener.class.getName());
sa.addContextParameter("contextConfigLocation", "beans.xml");
sa.setServletPath("/*");