SpringBoot实战分析(六)创建应用程序上下文
2018-06-19 16:35
344 查看
程序入口
context = createApplicationContext();
断点跟踪
1.判断环境类型和初始化
当前方法默认加载的是org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext这个类。
protected ConfigurableApplicationContext createApplicationContext() { Class<?> contextClass = this.applicationContextClass; if (contextClass == null) { try { switch (this.webApplicationType) { case SERVLET: contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS); break; case REACTIVE: contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS); break; default: contextClass = Class.forName(DEFAULT_CONTEXT_CLASS); } } 省略。。。。。。 } return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass); }contextClass的值域:
2.实例化类
获取AnnotationConfigServletWebServerApplicationContext声明的构造器,然后去按照构造器实例化。
public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException { Assert.notNull(clazz, "Class must not be null"); if (clazz.isInterface()) { throw new BeanInstantiationException(clazz, "Specified class is an interface"); } try { // 走clazz.getDeclaredConstructor()方法 Constructor<T> ctor = (KotlinDetector.isKotlinType(clazz) ? KotlinDelegate.getPrimaryConstructor(clazz) : clazz.getDeclaredConstructor()); return instantiateClass(ctor); } 省略。。。。。。 } public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException { Assert.notNull(ctor, "Constructor must not be null"); try { ReflectionUtils.makeAccessible(ctor); //走ctor.newInstance(args))方法 return (KotlinDetector.isKotlinType(ctor.getDeclaringClass()) ? KotlinDelegate.instantiateClass(ctor, args) : ctor.newInstance(args)); } 省略。。。。。。 }
3.返回context
相关文章推荐
- Spring boot实战中Application创建源码分析
- spring boot实战(第八篇)上下文的创建
- spring boot实战(第九篇)Application创建源码分析
- spring boot实战(第八篇)上下文的创建
- spring boot实战(第九篇)Application创建源码分析
- Android应用程序窗口(Activity)的运行上下文环境(Context)的创建过程分析
- SpringBoot实战(部署Spring Boot应用程序)读书笔记
- Spring Boot 揭秘与实战 源码分析 - 工作原理剖析
- Spring Boot实战与原理分析之简介篇
- Spring Boot实战与原理分析之快速入门
- spring boot实战(第六篇)加载application资源文件源码分析
- STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)
- STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)
- springBoot 创建bean失败问题分析
- spring boot实战(第十篇)Spring boot Bean加载源码分析
- spring boot学习二:Spring Boot自动装配分析与实战
- [Spring Boot实战系列] - No.1 开发第一个应用程序 Hello World
- spring boot实战(第六篇)加载application资源文件源码分析
- spring boot实战(第十篇)Spring boot Bean加载源码分析
- Android应用程序窗口(Activity)的运行上下文环境(Context)的创建过程分析