中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

寧波本地網(wǎng)站排行網(wǎng)絡(luò)推廣引流方式

寧波本地網(wǎng)站排行,網(wǎng)絡(luò)推廣引流方式,建設(shè)部網(wǎng)站注冊人員,小藍鳥pnv加速器😀前言 本篇博文是關(guān)于SpringBoot 底層機制分析實現(xiàn),希望能夠幫助你更好的了解SpringBoot 😊 🏠個人主頁:晨犀主頁 🧑個人簡介:大家好,我是晨犀,希望我的文章可以幫助到大…

😀前言
本篇博文是關(guān)于SpringBoot 底層機制分析實現(xiàn),希望能夠幫助你更好的了解SpringBoot 😊

🏠個人主頁:晨犀主頁
🧑個人簡介:大家好,我是晨犀,希望我的文章可以幫助到大家,您的滿意是我的動力😉😉

💕歡迎大家:這里是CSDN,我總結(jié)知識的地方,歡迎來到我的博客,感謝大家的觀看🥰
如果文章有什么需要改進的地方還請大佬不吝賜教 先在此感謝啦😊

文章目錄

  • 分析SpringBoot 底層機制【Tomcat 啟動分析+Spring 容器初始化+Tomcat 如何關(guān)聯(lián)Spring 容器】
    • 實現(xiàn)任務(wù)階段1- 創(chuàng)建Tomcat, 并啟動
      • ● 代碼實現(xiàn)
      • 完成測試
      • 運行效果
    • 實現(xiàn)任務(wù)階段2- 創(chuàng)建Spring 容器
      • ● 代碼實現(xiàn)
    • 實現(xiàn)任務(wù)階段3- 將Tomcat 和Spring 容器關(guān)聯(lián), 并啟動Spring 容器
      • ● 代碼實現(xiàn)
      • 完成測試
      • 注意事項和細節(jié)
    • 😄總結(jié)

分析SpringBoot 底層機制【Tomcat 啟動分析+Spring 容器初始化+Tomcat 如何關(guān)聯(lián)Spring 容器】

實現(xiàn)任務(wù)階段1- 創(chuàng)建Tomcat, 并啟動

說明: 創(chuàng)建Tomcat, 并啟動

● 代碼實現(xiàn)

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><!-- 導(dǎo)入springboot 父工程,規(guī)定的寫法解讀:1. springboot 我們指定2.5.3--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.3</version></parent><!-- 導(dǎo)入web 項目場景啟動器,會自動導(dǎo)入和web 開發(fā)相關(guān)依賴,非常方便--><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 去獲取依賴坐標(biāo).解讀:1. 使用指定的tomcat 才會驗證,效果高版本的tomcat默認(rèn)不會真正監(jiān)聽2. 使用了指定tomcat , 需要在spring-boot-starter-web 排除內(nèi)嵌的 starter-tomcat3. 否則會出現(xiàn)包沖突, 提示GenericServlet Not Found 類似錯誤--><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-core</artifactId><version>8.5.75</version></dependency></dependencies>
</project>

2 、創(chuàng)建nlc-springboot\src\main\java\com\nlc\nlcspringboot\NlcSpringApplication.java

public class NlcSpringApplication {//這里我們會創(chuàng)建tomcat對象,并關(guān)聯(lián)Spring容器, 并啟動public static void run() {try {//創(chuàng)建Tomcat對象 NlcTomcatTomcat tomcat = new Tomcat();//1. 讓tomcat可以將請求轉(zhuǎn)發(fā)到spring web容器,因此需要進行關(guān)聯(lián)//2. "/nlcboot" 就是我們的項目的 application context , 就是我們原來配置tomcat時,指定的application context//3. "D:\\nlc_springboot\\nlc-springboot" 指定項目的目錄tomcat.addWebapp("/nlcboot","D:\\nlc_springboot\\nlc-springboot");//設(shè)置9090tomcat.setPort(9090);//啟動tomcat.start();//等待請求接入System.out.println("======9090====等待請求=====");tomcat.getServer().await();} catch (Exception e) {e.printStackTrace();}}
}

3、創(chuàng)建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

實現(xiàn)任務(wù)階段2- 創(chuàng)建Spring 容器

說明: 創(chuàng)建Spring 容器

● 代碼實現(xiàn)

1 、創(chuàng)建nlc-springboot\src\main\java\com\nlc\nlcspringboot\bean\Monster.java , 做一個測試Bean

public class Monster {
}

2 、創(chuàng)建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 、創(chuàng)建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 、創(chuàng)建nlc-springboot\src\main\java\com\nlc\nlcspringboot\NlcWebApplicationInitializer.java , 作為Spring 的容器.

/*** 解讀* 1. 創(chuàng)建我們的Spring 容器* 2. 加載/關(guān)聯(lián)Spring容器的配置-按照注解的方式* 3. 完成Spring容器配置的bean的創(chuàng)建, 依賴注入* 4. 創(chuàng)建前端控制器 DispatcherServlet , 并讓其持有Spring容器* 5. 當(dāng)DispatcherServlet 持有容器, 就可以進行分發(fā)映射, 回憶我們實現(xiàn)SpringMVC底層機制* 6. 這里onStartup 是Tomcat調(diào)用, 并把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的創(chuàng)建和配置//1. 創(chuàng)建注冊非常重要的前端控制器 DispatcherServlet//2. 讓DispatcherServlet 持有容器//3. 這樣就可以進行映射分發(fā), 回憶一下SpringMvc機制[自己實現(xiàn)過]//NlcDispatcherServletDispatcherServlet dispatcherServlet = new DispatcherServlet(ac);//返回了ServletRegistration.Dynamic對象ServletRegistration.Dynamic registration =servletContext.addServlet("app", dispatcherServlet);//當(dāng)tomcat啟動時,加載 dispatcherServletregistration.setLoadOnStartup(1);//攔截請求,并進行分發(fā)處理//這里提示/ 和/*的配置,會匹配所有的請求,//當(dāng)Servlet 配置了"/", 會覆蓋tomcat 的DefaultServlet, 當(dāng)其他的utl-pattern 都匹配不上時, 都會走這個Servlet, 這樣可以攔截到其它靜態(tài)資源//這個默認(rèn)的servlet 是處理靜態(tài)資源的,一旦攔截,靜態(tài)資源不能處理//當(dāng)Servelt 配置了"/*", 表示可以匹配任意訪問路徑registration.addMapping("/");}
}

實現(xiàn)任務(wù)階段3- 將Tomcat 和Spring 容器關(guān)聯(lián), 并啟動Spring 容器

說明: 將Tomcat 和Spring 容器關(guān)聯(lián), 并啟動Spring 容器

● 代碼實現(xiàn)

  1. 修改nlc-springboot\src\main\java\com\nlc\nlcspringboot\NlcSpringApplication.java
public class NlcSpringApplication {//這里我們會創(chuàng)建tomcat對象,并關(guān)聯(lián)Spring容器, 并啟動public static void run() {try {//創(chuàng)建Tomcat對象 NlcTomcatTomcat tomcat = new Tomcat();//1. 讓tomcat可以將請求轉(zhuǎn)發(fā)到spring web容器,因此需要進行關(guān)聯(lián)//2. "/nlcboot" 就是我們的項目的 application context , 就是我們原來配置tomcat時,指定的application context//3. "D:\\nlc_springboot\\nlc-springboot" 指定項目的目錄tomcat.addWebapp("/nlcboot","D:\\nlc_springboot\\nlc-springboot");//設(shè)置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

注意事項和細節(jié)

1、如果啟動包異常, 如下:

嚴(yán)重: Servlet [jsp] in web application [/nlcboot] threw load() exception
java.lang.ClassNotFoundException: org.apache.jasper.servlet.JspServlet
2 、解決方案, 引入對應(yīng)版本的jasper 包即可, 修改nlc-springboot\pom.xml

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

😄總結(jié)

  1. 如果啟動包異常出現(xiàn)上述異常, 引入對應(yīng)版本的jasper 包就可以解決。
  2. 前面配置的application context可以根據(jù)自己的需求修改。
  3. 指定項目的目錄要根據(jù)自己的項目情況進行修改,否則會出現(xiàn)FileNotFoundException(系統(tǒng)找不到指定的文件)或NoSuchFileException(沒有此類文件)。

😁熱門專欄推薦
SpringBoot篇
SpringBoot 底層機制分析[上]
SpringBoot容器–注解的使用
SpringBoot 自動配置–常用配置
SpringBoot 依賴管理和自動配置—帶你了解什么是版本仲裁
Spring Boot介紹–快速入門–約定優(yōu)于配置

文章到這里就結(jié)束了,如果有什么疑問的地方請指出,諸大佬們一起來評論區(qū)一起討論😁
希望能和諸大佬們一起努力,今后我們一起觀看感謝您的閱讀🍻
如果幫助到您不妨3連支持一下,創(chuàng)造不易您們的支持是我的動力🤞

http://www.risenshineclean.com/news/21491.html

相關(guān)文章:

  • 太原做app網(wǎng)站建設(shè)國外網(wǎng)站排名前十
  • 不用dw怎么做網(wǎng)站西安網(wǎng)站維護
  • 網(wǎng)頁設(shè)計與制作教程江西高校出版社優(yōu)化設(shè)計答案六年級上冊語文
  • 河南專業(yè)網(wǎng)站建設(shè)日本產(chǎn)品和韓國產(chǎn)品哪個好
  • 網(wǎng)頁設(shè)計代碼模板網(wǎng)站企業(yè)網(wǎng)上的推廣
  • 網(wǎng)站常用架構(gòu)個人網(wǎng)站推廣方法
  • 武漢建設(shè)工程價格信息網(wǎng)杭州網(wǎng)站優(yōu)化體驗
  • 做時時彩網(wǎng)站牌照申請騰訊廣告
  • asp.net 價格查詢網(wǎng)站免費隱私網(wǎng)站推廣
  • 網(wǎng)站機房建設(shè)成本湖南網(wǎng)站seo營銷
  • 永城做網(wǎng)站利爾化學(xué)股票
  • 個人如何開網(wǎng)站賺錢平臺
  • 社區(qū)網(wǎng)站如何做長沙seo免費診斷
  • 網(wǎng)站前置審批流程seo建站公司
  • wordpress免費商城seo網(wǎng)站優(yōu)化軟件價格
  • 付費網(wǎng)站做推廣哪個好產(chǎn)品質(zhì)量推廣營銷語
  • 青海建設(shè)廳網(wǎng)站特種作業(yè)seo怎樣優(yōu)化網(wǎng)站
  • 百度推廣基木魚重慶seo網(wǎng)站管理
  • 肇慶網(wǎng)絡(luò)推廣公司重慶做優(yōu)化的網(wǎng)絡(luò)公司
  • 幫客戶做插邊球網(wǎng)站谷歌google地圖
  • 網(wǎng)站開發(fā)用技術(shù)seo優(yōu)化方案報價
  • 創(chuàng)意網(wǎng)紅墻圖片互聯(lián)網(wǎng)seo是什么
  • 阿里云網(wǎng)站備案好了 怎么建站阿里巴巴運營
  • 莆田的外貿(mào)網(wǎng)站營銷軟文范例
  • 中國菲律賓商會win7優(yōu)化大師官網(wǎng)
  • 特色的網(wǎng)站建設(shè)百度站長平臺官網(wǎng)登錄入口
  • 哪個網(wǎng)站專業(yè)做商鋪網(wǎng)站建設(shè)的一般步驟
  • 企業(yè)級網(wǎng)站開發(fā)項目教程西點培訓(xùn)學(xué)校
  • 國外商業(yè)網(wǎng)站建設(shè)無錫百度seo優(yōu)化
  • 做網(wǎng)站干什么用江蘇疫情最新消息