spring destroy-method + request scope bean

2019-08-03 04:29发布

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? 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.

I'm not really sure what to make of this, since my logging indicates the destroy method completed successfully. Does anyone know what's going on?

EDIT: Here's the 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>

2条回答
倾城 Initia
2楼-- · 2019-08-03 05:21

I never did get this working, but I ended up changing the code to apply @After advice on the controller methods, which has the same effect.

查看更多
在下西门庆
3楼-- · 2019-08-03 05:22

If you use request scope without spring MVC you should declare org.springframework.web.context.request.RequestContextListener in web-app listener.

<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

查看更多
登录 后发表回答