0

我可以在 spring bean xml 配置文件中配置 HttpSession 吗?我必须创建 HttpSession fectory,这样我就可以在任何地方使用所有会话对象。

有没有办法用xml配置文件将对象放在HttpSession下??

4

1 回答 1

0

你可以用DelegatingHttpSessionListenerProxy与框架类似的方式编写一个DelegatingFilterProxy.

例如(非常简单且未经测试)

public class DelegatingHttpSessionListenerProxy implements HttpSessionListener {
    private HttpSessionListener delegate;
    private WebApplicationContext wac;

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        if (wac == null) {
            wac = findWebApplicationContext(event);
        }

        if (wac != null) {
            delegate = wac.getBean(HttpSessionListener.class);
            delegate.sessionCreated(event);
        }
    }



    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        if (wac == null) {
            wac = findWebApplicationContext(event);
        }

        if (wac != null) {
            delegate = wac.getBean(HttpSessionListener.class);
            delegate.sessionDestroyed(event);
        }
    }

    protected WebApplicationContext findWebApplicationContext(HttpSessionEvent event) {
        return WebApplicationContextUtils.getWebApplicationContext(event.getSession().getServletContext());
    }

然后,在 web.xml 中注册代理并在应用程序上下文中管理您的实际实现。

于 2013-03-05T17:49:20.610 回答