启动Spring项⽬详细过程(⼩结)
1、Spring 项⽬放到web项⽬容器中(Tomcat、Jetty、JBoss)
本⽂以通⽤的Tomcat为例
2、项⽬容器启动时需要加载读取l配置⽂件
如下图:
3、容器⾸先会去读取l配置⽂件中的两个节点:<listener> </listener>和<context-param> </context-param>
说明:
tomcat在启动web容器的时候会启动⼀个叫ServletContextListener的,每当在web容器中有ServletContextListener这个接⼝被实例化的时候,web容器会通知ServletContextListener被实例的对象去执⾏其contextInitialized()的⽅法进⾏相应的业务处理;
⽽spring框架在设计的过程中ContextLoadListener这个类实现了ServletContextListener这个接⼝,因此每当有ContextLoadListener这个类被实例化的时候,web容器会通知Spring执⾏contextInitialized()这个⽅法,从⽽进⾏spring容器的启动与创建的过程中;
4、ContextLoaderListener中的contextInitialized()进⾏了spring容器的启动配置,调⽤initWebApplicationContext初始化spring容器;
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext());
}
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
//Spring 启动的句柄,spring容器开始启动的根⽬录
Attribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException("Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in l!");
} else {
Log logger = Log(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if(logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();
try {
//处理spring容器是否已经创建(只创建没有创建spring的各个bean)
t == null) {
}
t instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (t;
if(!cwac.isActive()) {
Parent() == null) {
ApplicationContext parent = this.loadParentContext(servletContext);
cwac.setParent(parent);
}
//Spring容器创建完成后,加载spring容器的各个组件
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, t);
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if(ccl == ClassLoader()) {
currentContext = t;
} else if(ccl != null) {
currentContextPerThread.put(ccl, t);
}
if(logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if(logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}
t;
} catch (RuntimeException var8) {
<("Context initialization failed", var8);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, var8);
throw var8;
} catch (Error var9) {
<("Context initialization failed", var9);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, var9);
throw var9;
}
}
}
5、spring容器创建完成后,准备开始实例化加载bean,Spring容器创建完成后,准备向spring容器中加载bean 使⽤configureAndRefreshWebApplicationContext(cwac, servletContext); 完成bean的加载;
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {
if (ObjectUtils.identityToString(wac).Id())) {
// The application context id is still set to its original default value
// -> assign a more useful id based on available information
String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
if (idParam != null) {
wac.setId(idParam);
}
else {
// Generate
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
}
}
wac.setServletContext(sc);
String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (configLocationParam != null) {
wac.setConfigLocation(configLocationParam);
}
/
/ The wac environment's #initPropertySources will be called in any case when the context
// is refreshed; do it eagerly here to ensure servlet property sources are in place for
// use in any post-processing or initialization that occurs below prior to #refresh
ConfigurableEnvironment env = Environment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(sc, null);
}
customizeContext(sc, wac);
}
说明:
configureAndRefreshWebApplicationContext中加载spring的配置⽂件,即l中读取<context-param></context-param>中加载到Spring的配置⽂件,即:
classpath:/l;
或
通过以下代码加载spring配置
public class Application{
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/l");
ctx.start();
}
}
此处略过如何调⽤DefaultResourceLoader
顶级接⼝ResourceLoader仅提供了⼀个getResource(String location)⽅法,可以根据⼀个资源地址加载资源⽂件,资源地址的表达式可以是以下⼏种:
--1. classpath:前缀开头的表达式,例如: l
spring启动流程面试回答--2.“/”开头的表达式,例如:/WEB-INF/l
--3. ⾮“/”开头的表达,例如:WEB-INF/l
--4. url协议,例如:file:/D:/ALANWANG-AIA/Horse-workspace/chapter3/target/l
Spring提供了实现类DefaultResourceLoader,DefaultResourceLoader在实现了以上列举的功能基础上,
还为开发者提供了⾃定义扩展接⼝ProtocolResolver,开发者可实现该接⼝定制个性化资源表达式,代码如下:
@Override
public Resource getResource(String location) {
for (ProtocolResolver protocolResolver : this.protocolResolvers) { // 1
Resource resource = solve(location, this);
if (resource != null) {return resource;}
}
if (location.startsWith("/")) {return getResourceByPath(location);} //2
else if (location.startsWith(CLASSPATH_URL_PREFIX)) { //3
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
}
else {
try {
// Try to parse the location as
URL url = new URL(location); //4
return new UrlResource(url);
}
catch (MalformedURLException ex) {
// No URL -> resolve as resource path.
return getResourceByPath(location); //5
}
}
}
步骤1,先⽤扩展协议解析器解析资源地址并返回。举个例⼦,咱们可以⾃定义资源解析器来完成带前缀“classpath:”的解析:
⾸先实现ProtocolResolver接⼝:
class ClasspathPreProtocolResolver implements ProtocolResolver{
private static String CLASS_PATH_PRE="classpath:";
public Resource resolve(String location, ResourceLoader resourceLoader) {
if( location.startsWith(CLASS_PATH_PRE)) {
return new ClassPathResource(location.substring(CLASS_PATH_PRE.length()));
}
return null;
}
}
步骤2,假设location以斜杠开头,则调⽤该类中 getResourceByPath(String path)⽅法,代码如下:
protected Resource getResourceByPath(String path) {
return new ClassPathContextResource(path, getClassLoader());
}
步骤三,假如资源表达式以classpath开头,则截取除前缀calsspath:的路径,并做为ClassPathResource的构造参数,⽣成ClassPathResource实例后返回。咱们可以在l中做如下配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/l</param-value>
</context-param>
6、通过refresh()内部的实现我们⼤致可以了解整个refresh()⽅法担负了整个Spring容器初始化和加载的所有逻辑,包括Bean⼯⼚的初始化、post-processor的注册以及调⽤、bean的实例化、事件发布等。以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论