【发布时间】: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