2011-04-12 216 views
1

所以我想要做这样的事情:春天破坏法+请求范围豆

@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> 
+0

我注意到DisposableBeanAdapter实现了Runnable ....它是否在线程中运行?这可以解释错误信息,例如http://stackoverflow.com/questions/1528444/accessing-scoped-proxy-beans-within-threads-of。然而,它并不完全有意义,因为在我的方法执行后错误显示*。 – Kevin 2011-04-12 17:42:55

+0

显示'dispatcher-servlet.xml' – Bozho 2011-04-12 18:54:55

+0

一切(几乎)都是注解配置驱动的,但我仍会附加。 – Kevin 2011-04-12 19:42:19

回答

0

我从来没有得到这个工作,但是我最终改变代码到控制器的方法,它具有相同的效果应用@After建议。

0

如果使用请求范围内无弹簧MVC,你应该在web应用程序监听声明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

+0

这不是问题,我想我不清楚。我正在使用spring MVC。 – Kevin 2011-04-12 17:37:40