Spring源码--两种启动方式
一. 通过编码方式启动Spring1. 通过FileSystemXmlApplicationContext启动spring容器public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws Beans...
一. 通过编码方式启动Spring
1. 通过FileSystemXmlApplicationContext启动spring容器
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}
2.通过ClassPathXmlApplicationContext启动spring容器
public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, ApplicationContext parent)
throws BeansException {
super(parent);
Assert.notNull(paths, "Path array must not be null");
Assert.notNull(clazz, "Class argument must not be null");
this.configResources = new Resource[paths.length];
for (int i = 0; i < paths.length; i++) {
this.configResources[i] = new ClassPathResource(paths[i], clazz);
}
refresh();
}
二. 通过Web容器方式启动Spring
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
/**
* Close the root web application context.
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
}
ServletContextListener 接口是Web容器暴露出来的接口,ContextLoaderListener 是它的一个实现。Web容器在启动时会去调用它的contextInitialized方法从而实现spring容器的启动。
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent ->
// determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
}
initWebApplicationContext方法的具体实现在父类ContextLoader类中。重点关注configureAndRefreshWebApplicationContext()方法。需要注意此处的cwac 默认情况下是XmlWebApplicationContext的实例,它是ConfigurableWebApplicationContext 的实现类。
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
ConfigurableEnvironment env = wac.getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
}
customizeContext(sc, wac);
wac.refresh();
}
可以看到,最后都有一个refresh()方法。此方法在AbstractApplicationContext类中有具体实现,然后在refresh()方法完成了对spring容器的启动。
更多推荐
所有评论(0)