??前言
本篇博文是關于SpringBoot 底層機制分析實現,希望能夠幫助你更好的了解SpringBoot ??
??個人主頁:晨犀主頁
??個人簡介:大家好,我是晨犀,希望我的文章可以幫助到大家,您的滿意是我的動力????
??歡迎大家:這里是CSDN,我總結知識的地方,歡迎來到我的博客,感謝大家的觀看??
如果文章有什么需要改進的地方還請大佬不吝賜教 先在此感謝啦??
分析SpringBoot 底層機制【Tomcat 啟動分析+Spring 容器初始化+Tomcat 如何關聯Spring 容器】
實現任務階段1- 創(chuàng)建Tomcat, 并啟動
說明: 創(chuàng)建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 父工程,規(guī)定的寫法
解讀:
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 開發(fā)相關依賴,非常方便-->
<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默認不會真正監(jiān)聽
2. 使用了指定tomcat , 需要在spring-boot-starter-web 排除內嵌的 starter-tomcat
3. 否則會出現包沖突, 提示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對象,并關聯Spring容器, 并啟動
public static void run() {
try {
//創(chuàng)建Tomcat對象 NlcTomcat
Tomcat tomcat = new Tomcat();
//1. 讓tomcat可以將請求轉發(fā)到spring web容器,因此需要進行關聯
//2. "/nlcboot" 就是我們的項目的 application context , 就是我們原來配置tomcat時,指定的application context
//3. "D:\\nlc_springboot\\nlc-springboot" 指定項目的目錄
tomcat.addWebapp("/nlcboot","D:\\nlc_springboot\\nlc-springboot");
//設置9090
tomcat.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();
}
}
完成測試
運行效果
瀏覽器請求, http://localhost:9090/ , 這時沒有返回信息
實現任務階段2- 創(chuàng)建Spring 容器
說明: 創(chuàng)建Spring 容器
● 代碼實現
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()
*/
@Bean
public Monster monster() {
return new Monster();
}
}
4 、創(chuàng)建nlc-springboot\src\main\java\com\nlc\nlcspringboot\NlcWebApplicationInitializer.java , 作為Spring 的容器.
/**
* 解讀
* 1. 創(chuàng)建我們的Spring 容器
* 2. 加載/關聯Spring容器的配置-按照注解的方式
* 3. 完成Spring容器配置的bean的創(chuàng)建, 依賴注入
* 4. 創(chuàng)建前端控制器 DispatcherServlet , 并讓其持有Spring容器
* 5. 當DispatcherServlet 持有容器, 就可以進行分發(fā)映射, 回憶我們實現SpringMVC底層機制
* 6. 這里onStartup 是Tomcat調用, 并把ServletContext 對象傳入
*/
public class NlcWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.out.println("startup ....");
//加載Spring web application configuration => 容器
//自己 寫過 NlcSpringApplicationContext
AnnotationConfigWebApplicationContext ac =
new AnnotationConfigWebApplicationContext();
//在ac中注冊 NlcConfig.class 配置類
ac.register(NlcConfig.class);
ac.refresh();//刷新上下文,完成bean的創(chuàng)建和配置
//1. 創(chuàng)建注冊非常重要的前端控制器 DispatcherServlet
//2. 讓DispatcherServlet 持有容器
//3. 這樣就可以進行映射分發(fā), 回憶一下SpringMvc機制[自己實現過]
//NlcDispatcherServlet
DispatcherServlet dispatcherServlet = new DispatcherServlet(ac);
//返回了ServletRegistration.Dynamic對象
ServletRegistration.Dynamic registration =
servletContext.addServlet("app", dispatcherServlet);
//當tomcat啟動時,加載 dispatcherServlet
registration.setLoadOnStartup(1);
//攔截請求,并進行分發(fā)處理
//這里提示/ 和/*的配置,會匹配所有的請求,
//當Servlet 配置了"/", 會覆蓋tomcat 的DefaultServlet, 當其他的utl-pattern 都匹配不上時, 都會走這個Servlet, 這樣可以攔截到其它靜態(tài)資源
//這個默認的servlet 是處理靜態(tài)資源的,一旦攔截,靜態(tài)資源不能處理
//當Servelt 配置了"/*", 表示可以匹配任意訪問路徑
registration.addMapping("/");
}
}
實現任務階段3- 將Tomcat 和Spring 容器關聯, 并啟動Spring 容器
說明: 將Tomcat 和Spring 容器關聯, 并啟動Spring 容器
● 代碼實現
- 修改nlc-springboot\src\main\java\com\nlc\nlcspringboot\NlcSpringApplication.java
public class NlcSpringApplication {
//這里我們會創(chuàng)建tomcat對象,并關聯Spring容器, 并啟動
public static void run() {
try {
//創(chuàng)建Tomcat對象 NlcTomcat
Tomcat tomcat = new Tomcat();
//1. 讓tomcat可以將請求轉發(fā)到spring web容器,因此需要進行關聯
//2. "/nlcboot" 就是我們的項目的 application context , 就是我們原來配置tomcat時,指定的application context
//3. "D:\\nlc_springboot\\nlc-springboot" 指定項目的目錄
tomcat.addWebapp("/nlcboot","D:\\nlc_springboot\\nlc-springboot");
//設置9090
tomcat.setPort(9090);
//啟動
tomcat.start();
//等待請求接入
System.out.println("======9090====等待請求=====");
tomcat.getServer().await();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- debug 一下, 看看是否進行Spring 容器的初始化工作, 可以看到ac.refresh() 會將NlcConfig.class 中配置Bean 實例化裝入到容器中…
里面有很多,可以自己看看
完成測試
1、啟動項目, 運行NlcMainApp
public class NlcMainApp {
public static void main(String[] args) {
//啟動NlcSpringBoot項目/程序
NlcSpringApplication.run();
}
}
2、運行的效果
注意事項和細節(jié)
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>
??總結
- 如果啟動包異常出現上述異常, 引入對應版本的jasper 包就可以解決。
- 前面配置的application context可以根據自己的需求修改。
- 指定項目的目錄要根據自己的項目情況進行修改,否則會出現FileNotFoundException(系統找不到指定的文件)或NoSuchFileException(沒有此類文件)。
??熱門專欄推薦
SpringBoot篇
SpringBoot 底層機制分析[上]
SpringBoot容器–注解的使用
SpringBoot 自動配置–常用配置
SpringBoot 依賴管理和自動配置—帶你了解什么是版本仲裁
Spring Boot介紹–快速入門–約定優(yōu)于配置文章來源:http://www.zghlxwxcb.cn/news/detail-636661.html
文章到這里就結束了,如果有什么疑問的地方請指出,諸大佬們一起來評論區(qū)一起討論??
希望能和諸大佬們一起努力,今后我們一起觀看感謝您的閱讀??
如果幫助到您不妨3連支持一下,創(chuàng)造不易您們的支持是我的動力??文章來源地址http://www.zghlxwxcb.cn/news/detail-636661.html
到了這里,關于SpringBoot 底層機制分析【Tomcat 啟動+Spring 容器初始化+Tomcat 如何關聯Spring 容器】【下】的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!