所以我想要做这样的事情:春天破坏法+请求范围豆
@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...");
}
}
}
我结束了以下消息之后,“已成功完成......”消息日志:
9时19分16秒WARN破坏方法的调用失败的名为“scopedTarget.myBean”豆:java.lang.IllegalStateException:没有线程绑定请求中找到:你是指请求属性实际web请求之外,或者处理原始接收线程之外的请求?如果实际上在Web请求中运行并仍然收到此消息,则代码可能在DispatcherServlet/DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter来公开当前请求。
我真的不知道该怎么做,因为我的日志记录表明destroy方法已成功完成。有谁知道发生了什么事?编辑: 这是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 ....它是否在线程中运行?这可以解释错误信息,例如http://stackoverflow.com/questions/1528444/accessing-scoped-proxy-beans-within-threads-of。然而,它并不完全有意义,因为在我的方法执行后错误显示*。 – Kevin 2011-04-12 17:42:55
显示'dispatcher-servlet.xml' – Bozho 2011-04-12 18:54:55
一切(几乎)都是注解配置驱动的,但我仍会附加。 – Kevin 2011-04-12 19:42:19