title: How can I get session from a common class in Spring Boot?
body: <p>I want to get session from a common class. Using <code>@Autowired</code> didn't work.</p>
  
  <pre><code>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
      }
  }
  </code></pre>
  
  <p>The constructor also didn't work</p>
  
  <pre><code>@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);
          ....
      }
  }
  </code></pre>
  
  <p>I also tried using <code>SpringBeanFactory</code>, it also didn't work.</p>
  
  <pre><code>@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 &lt;T&gt; T getBean(Class&lt;T&gt; clazz){
          return getApplicationContext().getBean(clazz);
      }
  
      //通过name,以及Clazz返回指定的Bean
      public static &lt;T&gt; T getBean(String name,Class&lt;T&gt; clazz){
          return getApplicationContext().getBean(name, clazz);
      }
  }
  </code></pre>
  
  <p><br>
  <code>SpringBeanFactoryUtil</code> only can get my custom bean, can't get <code>HttpSession</code>.<br>
  What should I do?</p>