【问题标题】:spring destroy-method + request scope beanspring 销毁方法 + 请求范围 bean
【发布时间】:2011-04-12 16:26:24
【问题描述】:

所以我想做这样的事情:

@Component
@Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES)
public class MyBean {
    @Autowired HttpServletRequest request;

    @PreDestroy 
    public void afterRequest() {
        try {
            System.out.println("After request...");
            // use request here:
        }
        finally {
            System.out.println("Completed successfully...");
        }
    }
}

在“成功完成...”消息日志之后,我得到以下消息:

09:19:16 WARN 在名为“scopedTarget.myBean”的 bean 上调用 destroy 方法失败:java.lang.IllegalStateException:未找到线程绑定请求:您指的是实际 Web 请求之外的请求属性,或在原始接收线程之外处理请求?如果您实际上是在 Web 请求中操作并且仍然收到此消息,则您的代码可能在 DispatcherServlet/DispatcherPortlet 之外运行:在这种情况下,请使用 RequestContextListener 或 RequestContextFilter 来公开当前请求。

我不确定该怎么做,因为我的日志记录表明销毁方法已成功完成。有谁知道怎么回事?

编辑: 这是mvc-servlet.xml。正如你所看到的,这里并没有发生太多事情。这都是注释驱动的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util-2.0.xsd">

<!-- properties file -->
<context:property-placeholder location="app.properties" />

<context:component-scan base-package="my.package.web" />
<context:component-scan base-package="my.package.services" />

<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/view" p:suffix=".jspx" />
</beans>

【问题讨论】:

  • 我注意到 DisposableBeanAdapter 实现了 Runnable....它在线程内运行吗?这可以解释错误消息,例如stackoverflow.com/questions/1528444/…。然而,它并不完全有意义,因为错误出现在之后我的方法已执行。
  • 显示dispatcher-servlet.xml
  • 一切(几乎)都是注释配置驱动的,但无论如何我都会附上。

标签: spring spring-mvc


【解决方案1】:

如果你使用没有 spring MVC 的请求范围,你应该在 web-app listener 中声明 org.springframework.web.context.request.RequestContextListener

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

查看http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-other-web-configuration

【讨论】:

  • 这不是问题,我想我不清楚。我正在使用 Spring MVC。
【解决方案2】:

我从来没有让这个工作,但我最终更改了代码以在控制器方法上应用 @After 建议,这具有相同的效果。

【讨论】:

    猜你喜欢
    • 2013-02-09
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2011-08-15
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多