1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.context;
  17. import javax.servlet.ServletContextEvent;
  18. import javax.servlet.ServletContextListener;
  19. /**
  20. * Bootstrap listener to start up Spring's root WebApplicationContext.
  21. * Simply delegates to ContextLoader.
  22. *
  23. * <p>This listener should be registered after Log4jConfigListener in web.xml,
  24. * if the latter is used.
  25. *
  26. * <p>For Servlet 2.2 containers and Servlet 2.3 ones that do not initalize
  27. * listeners before servlets, use ContextLoaderServlet. See the latter's Javadoc
  28. * for details.
  29. *
  30. * @author Juergen Hoeller
  31. * @since 17.02.2003
  32. * @see ContextLoader
  33. * @see ContextLoaderServlet
  34. * @see org.springframework.web.util.Log4jConfigListener
  35. */
  36. public class ContextLoaderListener implements ServletContextListener {
  37. private ContextLoader contextLoader;
  38. /**
  39. * Initialize the root web application context.
  40. */
  41. public void contextInitialized(ServletContextEvent event) {
  42. this.contextLoader = createContextLoader();
  43. this.contextLoader.initWebApplicationContext(event.getServletContext());
  44. }
  45. /**
  46. * Create the ContextLoader to use. Can be overridden in subclasses.
  47. * @return the new ContextLoader
  48. */
  49. protected ContextLoader createContextLoader() {
  50. return new ContextLoader();
  51. }
  52. /**
  53. * Close the root web application context.
  54. */
  55. public void contextDestroyed(ServletContextEvent event) {
  56. this.contextLoader.closeWebApplicationContext(event.getServletContext());
  57. }
  58. }