[英]spring destroy-method + request scope bean
So I wanted to do something like this: 所以我想做这样的事情:
@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...");
}
}
}
And I end up with the following message, AFTER the "Completed successfully..." message logs: 在出现“成功完成...”消息日志之后,我最终得到以下消息:
09:19:16 WARN Invocation of destroy method failed on bean with name 'scopedTarget.myBean': 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? 09:19:16 WARN在名称为'scopedTarget.myBean'的bean上调用destroy方法失败:java.lang.IllegalStateException:找不到线程绑定的请求:您是在引用实际Web请求之外的请求属性还是正在处理在原始接收线程之外请求? 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. 如果您实际上是在Web请求中操作并且仍然收到此消息,则您的代码可能在DispatcherServlet / DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter公开当前请求。
I'm not really sure what to make of this, since my logging indicates the destroy method completed successfully. 我不太确定该怎么做,因为我的日志记录表明destroy方法已成功完成。 Does anyone know what's going on? 有人知道发生了什么吗?
EDIT: Here's the mvc-servlet.xml
. 编辑:这是mvc-servlet.xml
。 As you can see there is not much going on here. 如您所见,这里没有太多活动。 It's all annotation driven: 全部由注释驱动:
<?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>
If you use request scope without spring MVC you should declare org.springframework.web.context.request.RequestContextListener
in web-app listener. 如果您在不使用Spring MVC的情况下使用请求范围,则应在网络应用监听器中声明org.springframework.web.context.request.RequestContextListener
。
<web-app>
...
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
...
</web-app>
check http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-other-web-configuration 检查http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-other-web-configuration
我从来没有做过这个工作,但是最终我更改了代码,以在控制器方法上应用@After建议,其效果相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.