Spring - Injecting a dependency into a ServletContextListener

前端 未结 4 1028
心在旅途
心在旅途 2020-11-27 16:59

I would like to inject a dependency into a ServletContextListener. However, my approach is not working. I can see that Spring is calling my setter method, but l

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 17:44

    I resolved this by removing the listener bean and creating a new bean for my properties. I then used the following in my listener, to get the properties bean:

    @Override
    public void contextInitialized(ServletContextEvent event) {
    
        final WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
        final Properties props = (Properties)springContext.getBean("myProps");
    }
    

提交回复
热议问题