Q:

Request scoped bean will not call @PreDestroy method

I've created a request scoped bean which should call a method once the request ends.
I tried to do so with a @PreDestroy annotated method, but this won't work.
@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SessionProvider {
    @PreDestroy
    public void cleanup() {
        // DO STUFF
    }
}

How can I achieve the described behavior?

A:

I've solved this issue by registering an own RequestContextListener:
public class MyRequestContextListener
        extends RequestContextListener {

    @Override
    public void requestDestroyed( ServletRequestEvent requestEvent ) {

        super.requestDestroyed( requestEvent );
        applicationContext.getBean( SessionProvider.class ).cleanup();
    }
}

Hopefully this helps someone else.
Best regards, Chris

