01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: * <p>For Servlet 2.2 containers and Servlet 2.3 ones that do not initalize
31: * listeners before servlets, use {@link ContextLoaderServlet}.
32: * See the latter's Javadoc for details.
33: *
34: * @author Juergen Hoeller
35: * @since 17.02.2003
36: * @see ContextLoaderServlet
37: * @see org.springframework.web.util.Log4jConfigListener
38: */
39: public class ContextLoaderListener implements ServletContextListener {
40:
41: private ContextLoader contextLoader;
42:
43: /**
44: * Initialize the root web application context.
45: */
46: public void contextInitialized(ServletContextEvent event) {
47: this .contextLoader = createContextLoader();
48: this .contextLoader.initWebApplicationContext(event
49: .getServletContext());
50: }
51:
52: /**
53: * Create the ContextLoader to use. Can be overridden in subclasses.
54: * @return the new ContextLoader
55: */
56: protected ContextLoader createContextLoader() {
57: return new ContextLoader();
58: }
59:
60: /**
61: * Return the ContextLoader used by this listener.
62: * @return the current ContextLoader
63: */
64: public ContextLoader getContextLoader() {
65: return this .contextLoader;
66: }
67:
68: /**
69: * Close the root web application context.
70: */
71: public void contextDestroyed(ServletContextEvent event) {
72: if (this.contextLoader != null) {
73: this.contextLoader.closeWebApplicationContext(event
74: .getServletContext());
75: }
76: }
77:
78: }
|