关于spring:个人博客系统开发系列一

1次阅读

共计 3762 个字符,预计需要花费 10 分钟才能阅读完成。

Preview

这个寒假原本好好地实习来着, 没成想疫情忽然在南京呈现了, 进一步的学习了下相干的常识, 在小破站跟着大神学习, 费了一番挫折, 做出了一个个别般的 demo, 不过可能胜利的部署到阿里云这件事件真的很让我开心, 嘴角疯狂上扬, 还发了一个链接给敌人, 难得的喜悦分享. 上面绝对这些学的只知其一; 不知其二的常识做一下略微零碎的梳理, 也算是为本人当前再做相干的额我的项目打一个根底.

技术工具采纳

  • SpringBoot
  • Thymeleaf
  • JPA
  • Semantic UI
  • html \ css \ js
  • jquery
  • 三方插件(css 成果,md 编辑显示)
  • 阿里云 ECS
  • maven
  • log4j
  • final shell
  • 宝塔面板

Springboot

springboot 是目前支流的 java 技术框架, 有很弱小的生态圈. 借助它官网的话来讲

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.

的确如此, 在很大水平上开发一个 demo 不是本人技术有多牛逼到哪儿去, 而是站在了伟人的肩膀上, 要是让本人把编译好字节码再分门别类错落有致的放到服务器对应的文件, 工作量的确很大. 比起 jsp 那种前后端耦合水平稍高, 接口提供麻烦的形式, 还是 springboot 这个货色比拟好, 不便了开发者去做本人的 demo, 然而同样也加剧了行业的竞争和学生的内卷水平, 哪天是个头.

Springboot 小点

这边看过一些剖析, 然而感觉各有差异, 而且版本更新很快, 很多货色都在做变动, 我是看着 2.5.3 的版本, 下载源码看了一下作者的注解, 有再加上其余相干的材料有了上面的一些理解. 其中有一些很重要的类, 上面列举几个.

SpringApplication

This Class that can be used to bootstrap and launch a Spring application from a Java main method. By default class will perform the following steps to bootstrap your application

通过它咱们能够来启动一个程序, 调用它的动态 run 办法 (其实是外部 new 出一个实例再应用) 是一种形式, 还有一些其余的形式也可能让咱们的 demo 跑起来.

ApplicationContextInitializer

Springboot启动流程

它的启动流程很简单, 可能干这么多事儿, 一是有了 @SpringApplication 这个注解, 另外就是下面提到的 SpringApplication 类,

1. 依据类门路创立一个 ApplicationContext 实例它的, 结构器还带有一个 primarySources 的参数, 这个实例里包含 监听器、初始化器,我的项目利用类型,启动类汇合,类加载器

Create an appropriate ApplicationContext instance (depending on your classpath)

断言 primarySources 不能为 null,如果为 null,抛出异样提醒

传入启动类的 Class

判断以后我的项目类型,有三种:NONE、SERVLET、REACTIVE

设置 ApplicationContextInitializer

设置监听器

判断主类,初始化入口类

2. 实例调用 run 办法 这个办法可真是太简单了


    public ConfigurableApplicationContext run(String... args) {
    // 创立计时器
    StopWatch stopWatch = new StopWatch();
    // 计时开始
    stopWatch.start();
    // 定义上下文对象
    DefaultBootstrapContext bootstrapContext = createBootstrapContext();
    ConfigurableApplicationContext context = null;
    //Headless 模式设置
    configureHeadlessProperty();
     // 加载 SpringApplicationRunListeners 监听器
    SpringApplicationRunListeners listeners = getRunListeners(args);
    // 发送 ApplicationStartingEvent 事件
    listeners.starting(bootstrapContext, this.mainApplicationClass);
    try {
        // 封装 ApplicationArguments 对象
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        // 配置环境模块
        ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
        // 依据环境信息配置要疏忽的 bean 信息
        configureIgnoreBeanInfo(environment);
        // 打印 Banner 标记 这边还能自定义 banner
        Banner printedBanner = printBanner(environment);
        // 创立 ApplicationContext 利用上下文
        context = createApplicationContext();
        context.setApplicationStartup(this.applicationStartup);
         //ApplicationContext 根本属性配置
        prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
        // 刷新上下文
        refreshContext(context);
        // 刷新后的操作,由子类去扩大
        afterRefresh(context, applicationArguments);
        // 计时完结
        stopWatch.stop();
        // 打印日志
        if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        // 发送 ApplicationStartedEvent 事件,标记 spring 容器曾经刷新,此时所有的 bean 实例都曾经加载结束
        listeners.started(context);
        // 查找容器中注册有 CommandLineRunner 或者 ApplicationRunner 的 bean,遍历并执行 run 办法
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        // 发送 ApplicationFailedEvent 事件,标记 SpringBoot 启动失
        handleRunFailure(context, ex, listeners);
        throw new IllegalStateException(ex);
    }

    try {
        // 发送 ApplicationReadyEvent 事件,标记 SpringApplication 曾经正在运行,即曾经胜利启动,能够接管服务申请。listeners.running(context);
    }
    catch (Throwable ex) {
        // 报告异样,然而不发送任何事件
        handleRunFailure(context, ex, null);
        throw new IllegalStateException(ex);
    }
    return context;
}

springboot 解决申请

这块儿其实挺头大的, 简化的应用底层是真的很简单, 这边帖一个官网的图示来略微讲下本人的了解

  1. DispatcherServlet(前端控制器)接管拦挡用户申请

    用户申请的 url 其实被分成了 服务器域名 我的项目名 controller 三个局部 可能还携带一些 参数 之类的

  2. DispatcherServlet 主动调用 HandlerMapping 处理器 映射 找到对应的Handler 并将 HandlerExecution 返回
  3. HandlerExecution 示意具体的 Handler
  4. DispatcherServlet 调用 HandlerAdapter去执行Handler
  5. Handler让具体的 Controller 执行一些 service 操作 增加一些数据到 dao 的 CRUD model对象的装载啥的
  6. 再通过 HandlerAdapter 把 model 信息啥的返回给HandlerAdapter
  7. HandlerAdapter调用ViewResolver 解析视图 像thymeleaf
  8. HandlerAdapter 返回 View 给浏览器 浏览器解析 view 出现给用户

集体公众号: 英短爱吃米 欢送关注

本文由博客一文多发平台 OpenWrite 公布!

正文完
 0