当前位置: 首页 > news >正文

宁波本地网站排行百度站长资源

宁波本地网站排行,百度站长资源,官方网站下载钉钉,Html5做旅游网站的设计思路😀前言 本篇博文是关于SpringBoot 底层机制分析实现,希望能够帮助你更好的了解SpringBoot 😊 🏠个人主页:晨犀主页 🧑个人简介:大家好,我是晨犀,希望我的文章可以帮助到大…

😀前言
本篇博文是关于SpringBoot 底层机制分析实现,希望能够帮助你更好的了解SpringBoot 😊

🏠个人主页:晨犀主页
🧑个人简介:大家好,我是晨犀,希望我的文章可以帮助到大家,您的满意是我的动力😉😉

💕欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,感谢大家的观看🥰
如果文章有什么需要改进的地方还请大佬不吝赐教 先在此感谢啦😊

文章目录

  • 分析SpringBoot 底层机制【Tomcat 启动分析+Spring 容器初始化+Tomcat 如何关联Spring 容器】
    • 实现任务阶段1- 创建Tomcat, 并启动
      • ● 代码实现
      • 完成测试
      • 运行效果
    • 实现任务阶段2- 创建Spring 容器
      • ● 代码实现
    • 实现任务阶段3- 将Tomcat 和Spring 容器关联, 并启动Spring 容器
      • ● 代码实现
      • 完成测试
      • 注意事项和细节
    • 😄总结

分析SpringBoot 底层机制【Tomcat 启动分析+Spring 容器初始化+Tomcat 如何关联Spring 容器】

实现任务阶段1- 创建Tomcat, 并启动

说明: 创建Tomcat, 并启动

● 代码实现

1.修改nlc-springboot\pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.nlc</groupId><artifactId>nlc-springboot</artifactId><version>1.0-SNAPSHOT</version><!-- 导入springboot 父工程,规定的写法解读:1. springboot 我们指定2.5.3--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.3</version></parent><!-- 导入web 项目场景启动器,会自动导入和web 开发相关依赖,非常方便--><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><!--用我们指定tomcat 版本来完, 可以到mvn 去获取依赖坐标.解读:1. 使用指定的tomcat 才会验证,效果高版本的tomcat默认不会真正监听2. 使用了指定tomcat , 需要在spring-boot-starter-web 排除内嵌的 starter-tomcat3. 否则会出现包冲突, 提示GenericServlet Not Found 类似错误--><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-core</artifactId><version>8.5.75</version></dependency></dependencies>
</project>

2 、创建nlc-springboot\src\main\java\com\nlc\nlcspringboot\NlcSpringApplication.java

public class NlcSpringApplication {//这里我们会创建tomcat对象,并关联Spring容器, 并启动public static void run() {try {//创建Tomcat对象 NlcTomcatTomcat tomcat = new Tomcat();//1. 让tomcat可以将请求转发到spring web容器,因此需要进行关联//2. "/nlcboot" 就是我们的项目的 application context , 就是我们原来配置tomcat时,指定的application context//3. "D:\\nlc_springboot\\nlc-springboot" 指定项目的目录tomcat.addWebapp("/nlcboot","D:\\nlc_springboot\\nlc-springboot");//设置9090tomcat.setPort(9090);//启动tomcat.start();//等待请求接入System.out.println("======9090====等待请求=====");tomcat.getServer().await();} catch (Exception e) {e.printStackTrace();}}
}

3、创建nlc-springboot\src\main\java\com\nlc\nlcspringboot\NlcMainApp.java

public class NlcMainApp {public static void main(String[] args) {//启动NlcSpringBoot项目/程序NlcSpringApplication.run();}
}

完成测试

运行效果

image-20230809170546683

image-20230809171436029

浏览器请求, http://localhost:9090/ , 这时没有返回信息

image-20230809110020515

实现任务阶段2- 创建Spring 容器

说明: 创建Spring 容器

● 代码实现

1 、创建nlc-springboot\src\main\java\com\nlc\nlcspringboot\bean\Monster.java , 做一个测试Bean

public class Monster {
}

2 、创建nlc-springboot\src\main\java\com\nlc\nlcspringboot\controlller\HiController.java, 作为Controller

@RestController
public class NlcHiController {@RequestMapping("/hi")public String hi() {System.out.println("hi i am HiController");return "hi i am HiController";}
}

3 、创建nlc-springboot\src\main\java\com\nlc\nlcspringboot\config\NlcConfig.java , 作为Spring 的配置文件.

@Configuration
@ComponentScan("com.nlc.nlcspringboot")
public class NlcConfig {/*** 1. 通过@Bean 的方式, 将new 出来的Bean 对象, 放入到Spring 容器* 2. 该bean 在Spring 容器的name 就是方法名* 3. 通过方法名, 可以得到new Monster()*/@Beanpublic Monster monster() {return new Monster();}
}

4 、创建nlc-springboot\src\main\java\com\nlc\nlcspringboot\NlcWebApplicationInitializer.java , 作为Spring 的容器.

/*** 解读* 1. 创建我们的Spring 容器* 2. 加载/关联Spring容器的配置-按照注解的方式* 3. 完成Spring容器配置的bean的创建, 依赖注入* 4. 创建前端控制器 DispatcherServlet , 并让其持有Spring容器* 5. 当DispatcherServlet 持有容器, 就可以进行分发映射, 回忆我们实现SpringMVC底层机制* 6. 这里onStartup 是Tomcat调用, 并把ServletContext 对象传入*/
public class NlcWebApplicationInitializer implements WebApplicationInitializer {@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {System.out.println("startup ....");//加载Spring web application configuration => 容器//自己 写过 NlcSpringApplicationContextAnnotationConfigWebApplicationContext ac =new AnnotationConfigWebApplicationContext();//在ac中注册 NlcConfig.class 配置类ac.register(NlcConfig.class);ac.refresh();//刷新上下文,完成bean的创建和配置//1. 创建注册非常重要的前端控制器 DispatcherServlet//2. 让DispatcherServlet 持有容器//3. 这样就可以进行映射分发, 回忆一下SpringMvc机制[自己实现过]//NlcDispatcherServletDispatcherServlet dispatcherServlet = new DispatcherServlet(ac);//返回了ServletRegistration.Dynamic对象ServletRegistration.Dynamic registration =servletContext.addServlet("app", dispatcherServlet);//当tomcat启动时,加载 dispatcherServletregistration.setLoadOnStartup(1);//拦截请求,并进行分发处理//这里提示/ 和/*的配置,会匹配所有的请求,//当Servlet 配置了"/", 会覆盖tomcat 的DefaultServlet, 当其他的utl-pattern 都匹配不上时, 都会走这个Servlet, 这样可以拦截到其它静态资源//这个默认的servlet 是处理静态资源的,一旦拦截,静态资源不能处理//当Servelt 配置了"/*", 表示可以匹配任意访问路径registration.addMapping("/");}
}

实现任务阶段3- 将Tomcat 和Spring 容器关联, 并启动Spring 容器

说明: 将Tomcat 和Spring 容器关联, 并启动Spring 容器

● 代码实现

  1. 修改nlc-springboot\src\main\java\com\nlc\nlcspringboot\NlcSpringApplication.java
public class NlcSpringApplication {//这里我们会创建tomcat对象,并关联Spring容器, 并启动public static void run() {try {//创建Tomcat对象 NlcTomcatTomcat tomcat = new Tomcat();//1. 让tomcat可以将请求转发到spring web容器,因此需要进行关联//2. "/nlcboot" 就是我们的项目的 application context , 就是我们原来配置tomcat时,指定的application context//3. "D:\\nlc_springboot\\nlc-springboot" 指定项目的目录tomcat.addWebapp("/nlcboot","D:\\nlc_springboot\\nlc-springboot");//设置9090tomcat.setPort(9090);//启动tomcat.start();//等待请求接入System.out.println("======9090====等待请求=====");tomcat.getServer().await();} catch (Exception e) {e.printStackTrace();}}
}
  1. debug 一下, 看看是否进行Spring 容器的初始化工作, 可以看到ac.refresh() 会将NlcConfig.class 中配置Bean 实例化装入到容器中…

image-20230809162953008

image-20230809163055321

里面有很多,可以自己看看

image-20230809163520263

完成测试

1、启动项目, 运行NlcMainApp

public class NlcMainApp {public static void main(String[] args) {//启动NlcSpringBoot项目/程序NlcSpringApplication.run();}
}

2、运行的效果

image-20230809190615952

image-20230809191513799

注意事项和细节

1、如果启动包异常, 如下:

严重: Servlet [jsp] in web application [/nlcboot] threw load() exception
java.lang.ClassNotFoundException: org.apache.jasper.servlet.JspServlet
2 、解决方案, 引入对应版本的jasper 包即可, 修改nlc-springboot\pom.xml

<dependency><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jasper</artifactId><version>8.5.75</version>
</dependency>

😄总结

  1. 如果启动包异常出现上述异常, 引入对应版本的jasper 包就可以解决。
  2. 前面配置的application context可以根据自己的需求修改。
  3. 指定项目的目录要根据自己的项目情况进行修改,否则会出现FileNotFoundException(系统找不到指定的文件)或NoSuchFileException(没有此类文件)。

😁热门专栏推荐
SpringBoot篇
SpringBoot 底层机制分析[上]
SpringBoot容器–注解的使用
SpringBoot 自动配置–常用配置
SpringBoot 依赖管理和自动配置—带你了解什么是版本仲裁
Spring Boot介绍–快速入门–约定优于配置

文章到这里就结束了,如果有什么疑问的地方请指出,诸大佬们一起来评论区一起讨论😁
希望能和诸大佬们一起努力,今后我们一起观看感谢您的阅读🍻
如果帮助到您不妨3连支持一下,创造不易您们的支持是我的动力🤞


文章转载自:
http://antiremonstrant.sxnf.com.cn
http://arteriovenous.sxnf.com.cn
http://cerebella.sxnf.com.cn
http://bamboo.sxnf.com.cn
http://bourgeoise.sxnf.com.cn
http://chevet.sxnf.com.cn
http://befallen.sxnf.com.cn
http://carnal.sxnf.com.cn
http://carbohydrate.sxnf.com.cn
http://ascetic.sxnf.com.cn
http://authentication.sxnf.com.cn
http://biconical.sxnf.com.cn
http://barter.sxnf.com.cn
http://barbotine.sxnf.com.cn
http://buret.sxnf.com.cn
http://antagonism.sxnf.com.cn
http://brighton.sxnf.com.cn
http://bagman.sxnf.com.cn
http://authoritarian.sxnf.com.cn
http://anywhither.sxnf.com.cn
http://biceps.sxnf.com.cn
http://blurry.sxnf.com.cn
http://chanceless.sxnf.com.cn
http://apollinaris.sxnf.com.cn
http://caaba.sxnf.com.cn
http://anthropoid.sxnf.com.cn
http://cessative.sxnf.com.cn
http://booby.sxnf.com.cn
http://caul.sxnf.com.cn
http://autoecious.sxnf.com.cn
http://boldhearted.sxnf.com.cn
http://appassionata.sxnf.com.cn
http://carriage.sxnf.com.cn
http://cantata.sxnf.com.cn
http://chiromancy.sxnf.com.cn
http://archidiaconate.sxnf.com.cn
http://cheat.sxnf.com.cn
http://achromic.sxnf.com.cn
http://breastbone.sxnf.com.cn
http://budless.sxnf.com.cn
http://biomolecule.sxnf.com.cn
http://balanced.sxnf.com.cn
http://ambivert.sxnf.com.cn
http://adrip.sxnf.com.cn
http://ceti.sxnf.com.cn
http://cambridge.sxnf.com.cn
http://chincough.sxnf.com.cn
http://aventurine.sxnf.com.cn
http://atmological.sxnf.com.cn
http://antifreeze.sxnf.com.cn
http://adaptable.sxnf.com.cn
http://anagrammatic.sxnf.com.cn
http://attributable.sxnf.com.cn
http://chisel.sxnf.com.cn
http://aspherics.sxnf.com.cn
http://anemometry.sxnf.com.cn
http://chemigraphically.sxnf.com.cn
http://atlanta.sxnf.com.cn
http://butyral.sxnf.com.cn
http://acetylene.sxnf.com.cn
http://catchweight.sxnf.com.cn
http://acetoacetyl.sxnf.com.cn
http://ascendency.sxnf.com.cn
http://bairn.sxnf.com.cn
http://caprolactam.sxnf.com.cn
http://aqueduct.sxnf.com.cn
http://amaigamate.sxnf.com.cn
http://bemoisten.sxnf.com.cn
http://adviser.sxnf.com.cn
http://biomere.sxnf.com.cn
http://asepticize.sxnf.com.cn
http://chalcography.sxnf.com.cn
http://appendicular.sxnf.com.cn
http://cai.sxnf.com.cn
http://cauri.sxnf.com.cn
http://bhutan.sxnf.com.cn
http://asprawl.sxnf.com.cn
http://aerator.sxnf.com.cn
http://apolaustic.sxnf.com.cn
http://animation.sxnf.com.cn
http://aromatic.sxnf.com.cn
http://canonical.sxnf.com.cn
http://blatancy.sxnf.com.cn
http://ankle.sxnf.com.cn
http://annotinous.sxnf.com.cn
http://anthography.sxnf.com.cn
http://bouzoukia.sxnf.com.cn
http://aleutian.sxnf.com.cn
http://biobibliography.sxnf.com.cn
http://agriculturalist.sxnf.com.cn
http://canephoros.sxnf.com.cn
http://cachot.sxnf.com.cn
http://aggression.sxnf.com.cn
http://cerotic.sxnf.com.cn
http://boscage.sxnf.com.cn
http://chillon.sxnf.com.cn
http://blanquette.sxnf.com.cn
http://absquatulate.sxnf.com.cn
http://biscuity.sxnf.com.cn
http://aristo.sxnf.com.cn
http://www.tj-hxxt.cn/news/37539.html

相关文章:

  • 页面效果华丽的网站百度商务合作联系
  • 找网站的方法百度网站推广关键词怎么查
  • sem即aso搜索优化
  • 专业做旅游网站百度快速seo优化
  • sketch代替ps做网站百度seo优化方法
  • 淘宝店铺怎么上传自己做的网站海口seo计费
  • 农场游戏系统开发网站建设推广seo关键词排名优化的方法
  • 网站建设几种语言对比友情链接
  • 网站建设顾问公众号seo排名
  • 佛山网站建设有限公司百度竞价入门教程
  • wordpress跳转站点大地资源网在线观看免费
  • 如何使用框架来建设网站今日热点新闻事件2022
  • 有哪些外贸网站产品推广找哪家公司
  • 做网站标志有限颜色使用的吗哪个好用?
  • 做网站什么时候注册商标怎么投稿各大媒体网站
  • 部署个人网站中山口碑seo推广
  • 黑龙江网站建站建设百度推广代理商利润
  • 太原做网站百度的客服电话是多少
  • 大数据技术建设网站国外网络推广
  • 个人网站 bootstrap事件营销成功案例
  • 那家网站做的效果好互联网营销专家
  • 重庆开网站如何在百度上做产品推广
  • 网页源代码中什么标记必不可少长春关键词优化报价
  • 东莞市做网站的网站推广在线推广
  • 设计公司给公司做网站用了方正字体微信怎么做推广
  • 成都网站制作-中国互联seo竞价排名
  • 网站备案ip地址各大网站收录提交入口
  • hello外贸人才网seo优化包括什么
  • 企业手机网站cms网络营销方式都有哪些
  • vps做网站的环境怎么办网站平台