如何从 Spring Boot 中的公共类获取会话? [英] How can I get session from a common class in Spring Boot?

查看:63
本文介绍了如何从 Spring Boot 中的公共类获取会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从普通班级获得会话.使用 @Autowired 不起作用.

I want to get session from a common class. Using @Autowired didn't work.

public class TMessageHandlerFactory implements MessageHandlerFactory {

    @Autowired
    private HttpSession session;

    @Override
    public void data(InputStream data) {
        int userId = (int)session.getAtrribute("key"); //session null
        ....        //do sth
    }
}

构造函数也不起作用

@Component
public class SMTPRunner implements ApplicationRunner {

    @Autowired
    private UserService userService;    // userService can access

    @Autowired
    private HttpSession session;        // session can't access

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        TMessageHandlerFactory myFactory = new TMessageHandlerFactory(session);
        ....
    }
}

我也尝试过使用 SpringBeanFactory,它也没有用.

I also tried using SpringBeanFactory, it also didn't work.

@Component
public class SpringBeanFactoryUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringBeanFactoryUtil.applicationContext == null) {
            SpringBeanFactoryUtil.applicationContext = applicationContext;
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}


SpringBeanFactoryUtil 只能获取我的自定义bean,不能获取HttpSession.
我该怎么办?


SpringBeanFactoryUtil only can get my custom bean, can't get HttpSession.
What should I do?

推荐答案

如果我理解正确,您想从范围更广的组件(单例)访问会话范围内的某些内容,因此系统无法知道您对服务器中的哪一个潜在并发会话感兴趣,实际上它会在 spring 初始化时说会话范围未定义.

If I understand you right, you want to access something in the session scope from a component that a broader scope (singleton), as such the system can't know which one of the potential concurrent sessions in ther server you are interrested in an practically it would say at spring init time that the session scope isn't defined.

您可以使用 ObjectFactory 模式(可能的解决方案之一)解决此问题

You can get arround that with the ObjectFactory pattern (1 of the possible solution)

import `org.springframework.beans.factory.ObjectFactory`;
// ...
// ...

@Autowired
ObjectFactory<HttpSession> httpSessionFactory;

然后当你需要它时,从绑定到会话的线程:

And then when you need it, from a thread that is bound to the session:

HttpSession session = httpSessionFactory.getObject();

通过这种方式,spring 绑定了一个receipe,以在您调用getObject() 方法的类型中获取您需要的对象,而不是尚未可用的实际对象.

This way spring bind a receipe to get the object you need at the type you call the getObject() method rather than the actual object that is not yet available.

请理解,如果在您运行代码时没有会话绑定到当前线程,这将失败(返回 null),因为没有会话可用.这意味着您要么从未能转发请求/会话的请求线程本地信息的线程调用此代码,要么从没有意义的上下文调用此代码.

Please understand that if there no session bound to the current thread when you run the code, this will fail (return null) as no session is available. This mean either you call this code from a thread that you failed to forward the request thread local information of the request/session or you call this code from a context where it doesn't make sense.

这篇关于如何从 Spring Boot 中的公共类获取会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆