美文网首页
深入Spring Boot 2.0(1) 应用的创建

深入Spring Boot 2.0(1) 应用的创建

作者: 个人为痕迹 | 来源:发表于2018-05-30 21:58 被阅读0次

    我是从2017年上半年大三下半年实习的时候开始接触Spring Boot,在最近一段时间更新到了2.0,最早接触它为的是的简便配置加快开发,放弃笨重的XML配置的方式。在随后的过程中我遇到了许许多多的问题,在此与各位一起探讨,如有错误欢迎指出,首先Spring Boot的唯一入口主方法类位于项目创建后的包内,结构一般如下(在创建时选择war方式可能稍有区别,会另生成一个ServletInitializer类,但并不妨碍接下去的阅读):

    @SpringBootApplication
    public class NewpowerApplication {
          public static void main(String[] args) {
                SpringApplication.run(NewpowerApplication.class, args);
          }
    }
    

    在main方法中调用org.springframework.boot:spring-boot中的SpringApplication类的run方法(代码如下)

    public static ConfigurableApplicationContext run(Class[] primarySources, String[] args) {
       return (new SpringApplication(primarySources)).run(args);
    }
    

    传入的参数为当前的Class和args,在SpringApplication中我们发现其中还有一个main方法:

    public static void main(String[] args) throws Exception {
        SpringApplication.run(new Class<?>[0], args);
    }
    

    在注释中我们可以得知开发者可以通过application.properties或者yml文件中的spring.main.sources参数配置命令行参数来运行应用,有兴趣的同学可以测试一下,args在学习Java基础时都有讲到过是命令行运行时传递的字符串数组,在后续文章中我会为大家介绍此参数的作用。

    run 方法首先将入口类作为参数传入SpringApplication构造器new 出实例在创建实例的过程中分为一下几步,首先确认应用的类型通过deduceWebApplicationType()方法判断类型

     private WebApplicationType deduceWebApplicationType() {
           if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS,null)
                && !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS,null)) {
                      return WebApplicationType.REACTIVE;
           }
           for (String className :WEB_ENVIRONMENT_CLASSES) {
                      if (!ClassUtils.isPresent(className,null)) {
                                  return WebApplicationType.NONE;
                       }
           }
           return WebApplicationType.SERVLET;
     }
    

    其中通过ClassUtil类查询应用中是否存在对应的类来判断应用的类型,在源码中读者可能会对isPresent()方法第二个参数ClassLoad参数为空比较疑惑,读者可以查看相关源码和Java的ClassLoader的相关资料进行了解Java类的加载。接下来就是设置应用上下文初始化器集合的过程,通过getSpringFactoriesInstances()方法将创建的实例集合设置给当前SpringApplication实例的initializers属性,根据源码我们可以在依赖包org.springframework.boot:spring-boot根目录下的META-INF/spring.factories文件中找到具体设置的实例的类型,内容如下:

    spring.factories的部分内容

    接下来是设置监听器集合的过程与上一个步骤相似就不在重复,最后就是设置主应用类了通过deduceMainApplicationClass()方法获取类

    private Class deduceMainApplicationClass() {
       try {
          //获取所有调用者的所有的栈信息
          StackTraceElement[] stackTrace =new RuntimeException().getStackTrace();
          for (StackTraceElement stackTraceElement : stackTrace) {
                  //通过判断main方法的信息获取main方法的类
                  if ("main".equals(stackTraceElement.getMethodName())) {
                                 return Class.forName(stackTraceElement.getClassName());
                  }
           }
       }catch (ClassNotFoundException ex) {
           // Swallow and continue
       }
       return null;
    }
    

    这样Spring应用的实例就已经创建完毕了。关于应用的运行我将在第二部分进行深入

    相关文章

      网友评论

          本文标题:深入Spring Boot 2.0(1) 应用的创建

          本文链接:https://www.haomeiwen.com/subject/lzaxsftx.html