I want to get session from a common class. @Autowired is 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
}
}
the constructor also didn't work
@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);
....
}
}
Then I try use 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 only can get my custom bean, can't get httpsession. How should I do?