【问题标题】:can HttpSession object configured in xml bean java spring可以在xml bean java spring中配置HttpSession对象吗
【发布时间】:2013-03-05 15:45:10
【问题描述】:

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

有什么办法可以用xml配置文件把对象放在HttpSession下吗??

【问题讨论】:

  • 容器管理 http 会话,所以 spring 不应该参与其中。

标签: java spring model-view-controller javabeans httpsession


【解决方案1】:

您可以编写一个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 中注册代理并在应用程序上下文中管理您的实际实现。

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 2018-12-24
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    相关资源
    最近更新 更多