@Autowired in ServletContextListener

前端 未结 4 1617
一生所求
一生所求 2021-02-06 06:04

i hava aclass InitApp

@Component
public class InitApp implements ServletContextListener {

@Autowired
ConfigrationService weatherConfService;

/** Creates a new          


        
相关标签:
4条回答
  • 2021-02-06 06:39

    By declaring

    <listener>
      <listener-class>com.web.Utils.InitApp</listener-class>
    </listener>
    

    in your web.xml, you're telling your container to initialize and register an instance of InitApp. As such, that object is not managed by Spring and you cannot @Autowired anything into it.

    If your application context is set up to component-scan the com.web.Utils package, then you will have a InitApp bean that isn't registered as a Listener with the container. So even though your other bean will be @Autowired, the servlet container won't ever hit it.

    That is the trade-off.

    There are workarounds to this if you use programmatic configuration with a ServletContainerInitializer or a WebApplicationInitializer for servlet 3.0. You wouldn't use @Autowired, you would just have setter/getter that you would use to set the instance.

    Here's an example

    class SpringExample implements WebApplicationInitializer {
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            WebApplicationContext context = ...;
    
            SomeBean someBean = context.getBean(SomeBean.class);
            CustomListener listener = new CustomListener();
            listener.setSomeBean(someBean);
    
            // register the listener instance
            servletContext.addListener(listener);
    
            // then register DispatcherServlet and ContextLoaderListener, as appropriate
            ...
        }
    }
    
    class CustomListener implements ServletContextListener {
        private SomeBean someBean;
    
        public SomeBean getSomeBean() {
            return someBean;
        }
    
        public void setSomeBean(SomeBean someBean) {
            this.someBean = someBean;
        }
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
           // do something with someBean
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
        }
    }
    

    Note that Spring has some custom implementation of WebApplicationInitializer that are quite sophisticated. You really shouldn't need to implement it directly yourself. The idea remains the same, just deeper in the inheritance hierarchy.

    0 讨论(0)
  • 2021-02-06 06:44

    A couple of ideas came to me as I was having the same issue.

    First one is to use Spring utils to retrieve the bean from the Spring context within the listener:

    Ex:

    @WebListener
    public class CacheInitializationListener implements ServletContextListener {
    
        /**
         * Initialize the Cache Manager once the application has started
         */
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            CacheManager cacheManager = WebApplicationContextUtils.getRequiredWebApplicationContext(
                    sce.getServletContext()).getBean(CacheManager.class);
            try {
                cacheManager.init();
            } catch (Exception e) {
                // rethrow as a runtime exception
                throw new IllegalStateException(e);
            }
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            // TODO Auto-generated method stub
    
        }
    }
    

    This works fine if you only have one or two beans. Otherwise it can get tedious. The other option is to explicitly call upon Spring's Autowire utilities:

    @WebListener
    public class CacheInitializationListener implements ServletContextListener {
    
        @Autowired
        private CacheManager cacheManager;
    
        /**
         * Initialize the Cache once the application has started
         */
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            try {
                cacheManager.init();
            } catch (Exception e) {
                // rethrow as a runtime exception
                throw new IllegalStateException(e);
            }
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            // TODO Auto-generated method stub
    
        }
    }
    

    The caveat in both these solutions, is that the Spring context must by loaded first before either of these can work. Given that there is no way to define the Listener order using @WebListener, ensure that the Spring ContextLoaderListener is defined in web.xml to force it to be loaded first (listeners defined in the web descriptor are loaded prior to those defined by annotation).

    0 讨论(0)
  • 2021-02-06 06:47

    As others have said this listener observes by the web servlet(tomcat) context (Not the Spring Container) and is notified of servlet startup/shutdown.

    Since it is created by the servlet outside of the Spring container it is not managed by Spring hence @Autowire members is not possible.

    If you setup your bean as a managed @Component then Spring will create the instance and the listener wont register with the external servlet.

    You cannot have it both ways..

    One solution is the remove the Spring annotations and manually retrieve your member from the Spring Application context and set your members that way.

    ie

        public class InitApp implements ServletContextListener {
    
            private ConfigrationService weatherConfService;
    
            private static ApplicationContext   applicationContext  = new ClassPathXmlApplicationContext("classpath:web-context.xml");
    
            @Override
            public void contextInitialized(ServletContextEvent servletContextEvent) {
                weatherConfService = applicationContext.getBean(ConfigrationService.class);
                System.out.println(weatherConfService);
            }
    
            @Override
            public void contextDestroyed(ServletContextEvent servletContextEvent) {
            }
        }
    
    0 讨论(0)
  • 2021-02-06 07:02
    @WebListener
    public class StartupListener implements ServletContextListener {
    
        @Autowired
        private MyRepository repository;
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
        }
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            AutowireCapableBeanFactory autowireCapableBeanFactory = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext()).getAutowireCapableBeanFactory();
            autowireCapableBeanFactory.autowireBean(this);
    
            repository.doSomething();
        }
    }
    
    0 讨论(0)
提交回复
热议问题