1 /*
2 * Copyright 2002-2007 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
17 package org.springframework.web.context;
18
19 import javax.servlet.ServletContextEvent;
20 import javax.servlet.ServletContextListener;
21
22 /**
23 * Bootstrap listener to start up Spring's root {@link WebApplicationContext}.
24 * Simply delegates to {@link ContextLoader}.
25 *
26 * <p>This listener should be registered after
27 * {@link org.springframework.web.util.Log4jConfigListener}
28 * in <code>web.xml</code>, if the latter is used.
29 *
30 * @author Juergen Hoeller
31 * @since 17.02.2003
32 * @see ContextLoaderServlet
33 * @see org.springframework.web.util.Log4jConfigListener
34 */
35 public class ContextLoaderListener implements ServletContextListener {
36
37 private ContextLoader contextLoader;
38
39
40 /**
41 * Initialize the root web application context.
42 */
43 public void contextInitialized(ServletContextEvent event) {
44 this.contextLoader = createContextLoader();
45 this.contextLoader.initWebApplicationContext(event.getServletContext());
46 }
47
48 /**
49 * Create the ContextLoader to use. Can be overridden in subclasses.
50 * @return the new ContextLoader
51 */
52 protected ContextLoader createContextLoader() {
53 return new ContextLoader();
54 }
55
56 /**
57 * Return the ContextLoader used by this listener.
58 * @return the current ContextLoader
59 */
60 public ContextLoader getContextLoader() {
61 return this.contextLoader;
62 }
63
64
65 /**
66 * Close the root web application context.
67 */
68 public void contextDestroyed(ServletContextEvent event) {
69 if (this.contextLoader != null) {
70 this.contextLoader.closeWebApplicationContext(event.getServletContext());
71 }
72 }
73
74 }