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解决申请
这块儿其实挺头大的,简化的应用底层是真的很简单,这边帖一个官网的图示来略微讲下本人的了解
-
DispatcherServlet(前端控制器)接管拦挡用户申请
用户申请的url 其实被分成了 服务器域名 我的项目名 controller三个局部 可能还携带一些参数之类的
- DispatcherServlet 主动调用 HandlerMapping 处理器 映射 找到对应的Handler 并将 HandlerExecution 返回
- HandlerExecution 示意具体的 Handler
- DispatcherServlet 调用 HandlerAdapter去执行Handler
- Handler让具体的Controller执行一些service操作 增加一些数据到dao 的CRUD model对象的装载啥的
- 再通过HandlerAdapter把model信息啥的返回给HandlerAdapter
- HandlerAdapter调用ViewResolver 解析视图 像thymeleaf
- HandlerAdapter 返回View给浏览器 浏览器解析view出现给用户
集体公众号:英短爱吃米 欢送关注
本文由博客一文多发平台 OpenWrite 公布!
发表回复