zdd123456的gravatar头像
zdd123456 2018-01-03 18:03:38

spring boot 如何配置RequestContextListener

如题,我想在spring boot配置 

RequestContextListener,用来初始化项目后加载数据到内存里,但是现在问题是获取的是空指针。我的代码如下:

@Configuration
public class Listener {
        @Bean
        public ServletListenerRegistrationBean<EventListener> getDemoListener(){
            ServletListenerRegistrationBean<EventListener> registrationBean
                                       =new ServletListenerRegistrationBean<>();
            registrationBean.setListener(new RequestContextListener());
    //        registrationBean.setOrder(1);
            return registrationBean;
        }
}

 

所有回答列表(2)
sunjiyun26的gravatar头像
sunjiyun26  LV9 2018年1月8日

in application main method

@Bean
 public RequestContextListener requestContextListener(){
    return new RequestContextListener();
} 

your own listner

public class MyAdditionListeners extends SpringBootServletInitializer {

    protected final Log logger = LogFactory.getLog(getClass());

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
        if (rootAppContext != null) {
            servletContext.addListener(new YourListenerHere());
        }
        else {
            this.logger.debug("No ContextLoaderListener registered, as "
                    + "createRootApplicationContext() did not "
                    + "return an application context");
        }
    }

 

评论(0) 最佳答案
sunjiyun26的gravatar头像
sunjiyun26  LV9 2018年1月8日

in application main method

@Bean
 public RequestContextListener requestContextListener(){
    return new RequestContextListener();
} 

 

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友