之前Spring相关的一些类,比如Enviromnent,BenFactory都接触了一些.有一些收获.但是直接看代码很多方法都不知为什么这样写.哪里会用到.因为太底层了.不好理解..现在从高层次的调用开始再研究下.应该会有新的理解.
所以从一个Web应用初始化开始学习.看看它经历了哪些步骤.做了哪些事情.
之前对spring的dispatcherServlet有一点点研究(http://www.cnblogs.com/abcwt112/p/5283674.html).
ContextLoaderListener
1个最普通的WEB项目如果要在servlet环境中用Spring.肯定是在web.xml里配置1个listener.这个linstener是1个入口,在内部肯定会创建Spring相关的applicationcontext并配置它.
1 /* 2 * Copyright 2002-2015 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 and shut down Spring's root {@link WebApplicationContext}. 24 * Simply delegates to {@link ContextLoader} as well as to {@link ContextCleanupListener}. 25 * 26 * <p>This listener should be registered after {@link org.springframework.web.util.Log4jConfigListener} 27 * in {@code web.xml}, if the latter is used. 28 * 29 * <p>As of Spring 3.1, {@code ContextLoaderListener} supports injecting the root web 30 * application context via the {@link #ContextLoaderListener(WebApplicationContext)} 31 * constructor, allowing for programmatic configuration in Servlet 3.0+ environments. 32 * See {@link org.springframework.web.WebApplicationInitializer} for usage examples. 33 * 34 * @author Juergen Hoeller 35 * @author Chris Beams 36 * @since 17.02.2003 37 * @see org.springframework.web.WebApplicationInitializer 38 * @see org.springframework.web.util.Log4jConfigListener 39 */ 40 public class ContextLoaderListener extends ContextLoader implements ServletContextListener { 41 42 /** 43 * Create a new {@code ContextLoaderListener} that will create a web application 44 * context based on the "contextClass" and "contextConfigLocation" servlet 45 * context-params. See {@link ContextLoader} superclass documentation for details on 46 * default values for each. 47 * <p>This constructor is typically used when declaring {@code ContextLoaderListener} 48 * as a {@code <listener>} within {@code web.xml}, where a no-arg constructor is 49 * required. 50 * <p>The created application context will be registered into the ServletContext under 51 * the attribute name {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} 52 * and the Spring application context will be closed when the {@link #contextDestroyed} 53 * lifecycle method is invoked on this listener. 54 * @see ContextLoader 55 * @see #ContextLoaderListener(WebApplicationContext) 56 * @see #contextInitialized(ServletContextEvent) 57 * @see #contextDestroyed(ServletContextEvent) 58 */ 59 public ContextLoaderListener() { 60 } 61 62 /** 63 * Create a new {@code ContextLoaderListener} with the given application context. This 64 * constructor is useful in Servlet 3.0+ environments where instance-based 65 * registration of listeners is possible through the {@link javax.servlet.ServletContext#addListener} 66 * API. 67 * <p>The context may or may not yet be {@linkplain 68 * org.springframework.context.ConfigurableApplicationContext#refresh() refreshed}. If it 69 * (a) is an implementation of {@link ConfigurableWebApplicationContext} and 70 * (b) has <strong>not</strong> already been refreshed (the recommended approach), 71 * then the following will occur: 72 * <ul> 73 * <li>If the given context has not already been assigned an {@linkplain 74 * org.springframework.context.ConfigurableApplicationContext#setId id}, one will be assigned to it</li> 75 * <li>{@code ServletContext} and {@code ServletConfig} objects will be delegated to 76 * the application context</li> 77 * <li>{@link #customizeContext} will be called</li> 78 * <li>Any {@link org.springframework.context.ApplicationContextInitializer ApplicationContextInitializer}s 79 * specified through the "contextInitializerClasses" init-param will be applied.</li> 80 * <li>{@link org.springframework.context.ConfigurableApplicationContext#refresh refresh()} will be called</li> 81 * </ul> 82 * If the context has already been refreshed or does not implement 83 * {@code ConfigurableWebApplicationContext}, none of the above will occur under the 84 * assumption that the user has performed these actions (or not) per his or her 85 * specific needs. 86 * <p>See {@link org.springframework.web.WebApplicationInitializer} for usage examples. 87 * <p>In any case, the given application context will be registered into the 88 * ServletContext under the attribute name {@link 89 * WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} and the Spring 90 * application context will be closed when the {@link #contextDestroyed} lifecycle 91 * method is invoked on this listener. 92 * @param context the application context to manage 93 * @see #contextInitialized(ServletContextEvent) 94 * @see #contextDestroyed(ServletContextEvent) 95 */ 96 public ContextLoaderListener(WebApplicationContext context) { 97 super(context); 98 } 99 100 101 /** 102 * Initialize the root web application context. 103 */ 104 @Override 105 public void contextInitialized(ServletContextEvent event) { 106 initWebApplicationContext(event.getServletContext()); 107 } 108 109 110 /** 111 * Close the root web application context. 112 */ 113 @Override 114 public void contextDestroyed(ServletContextEvent event) { 115 closeWebApplicationContext(event.getServletContext()); 116 ContextCleanupListener.cleanupAttributes(event.getServletContext()); 117 } 118 119 }