簡易版SpringBoot自定義模擬實現(xiàn)
SpringBoot作為目前最流行的框架之一,同時是每個程序員必須掌握的知識,其提供了豐富的功能模塊和開箱即用的特性,極大地提高了開發(fā)效率和降低了學習成本,使得開發(fā)人員能夠更專注于業(yè)務邏輯的實現(xiàn),而無需過多關注底層框架的配置和集成。本文模擬實現(xiàn)簡易版SpringBoot。
模塊創(chuàng)建
創(chuàng)建一個Springboot源碼模塊,主要用來實現(xiàn)SpringBoot的核心編程邏輯,類似導入SpringBoot依賴。
創(chuàng)建一個應用模塊Demo,用來實現(xiàn)業(yè)務邏輯測試我們自己編寫好的Springboot代碼。
依賴導入
由于SpringBoot是依賴于Spring的也依賴SpringMVC,所以我們也得依賴Spring和SpringMVC,導入Spring與SpringMVC的相關jar。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.60</version>
</dependency>
</dependencies>而Demo模塊就可以類似平常一樣,隨便寫需要什么導入什么,但是得依賴于我們自己寫的SpringBoot模塊。
<dependencies>
<dependency>
<groupId>com.simulate.example</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>代碼編寫
Demo模塊的代碼直接就正常編寫邏輯,定義一個Controller,Service一個接口請求方法執(zhí)行“/test”。
SpringBoot模塊,效仿真正的SpringBoot項目在項目啟動類里面存在一個注解,傳入配置類,然后調(diào)用run方法即可。
/**
* @author dream
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Configuration
@ComponentScan
public @interface DemoSpringBootApplication {}
public class MySpringApplication {
public static void run(Class clazz){
}
}首先我們需要去定義一個核心的注解類和一個啟動類DemoSpringApplication。
定義完這兩個類此時我們就可以去編寫Demo業(yè)務的啟動類,之前是表示@SpringBootApplication,現(xiàn)在通過我們自定義的注解來實現(xiàn)。
@DemoSpringBootApplication
public class MyApplication {
public static void main(String[] args) {
MySpringApplication.run(MyApplication.class);
}
}實現(xiàn)run方法邏輯
我想著當run方法結束后,我們就可以在瀏覽器里面訪問我們之前定義好的test路徑,那么run方法必定會去啟動Tomcat服務才能夠在瀏覽器里面訪問,所在方法里面必須去啟動一個Tomcat服務。
同時我們需要掃描得到Spring的相關類,同時還得利用Springmvc去進行相關操作,將DispatcherServlet加入到Tomcat中。
在run方法里面需要實現(xiàn)邏輯:創(chuàng)建一個Spring容器,創(chuàng)建Tomcat對象,創(chuàng)建DispatcherServlet對象并且和前面創(chuàng)建出來的Spring容器進行綁定將DispatcherServlet添加到Tomcat中,最后啟動Tomcat。
創(chuàng)建Spring容器
public static void run(Class clazz) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(clazz);
applicationContext.refresh();
}創(chuàng)建AnnotationConfigWebApplicationContext容易傳入class類就表示該clazz為配置類,Spring就會去掃描類上的相關注解,這時候就會掃描到我們自己寫好的@DemoSpringBootApplication注解,然后該注解內(nèi)存存在@ComponentScan注解等都會一并去掃描實現(xiàn),ComponentScan就是去掃描路徑找到bean對象,如果沒有指定路徑默認就是配置類所在包路徑,就會將Demo的Controller類掃描到Spring中,并將訪問地址掃描到其中。
創(chuàng)建Tomcat對象與DispatcherServlet并綁定啟動
public static void startTomcat(WebApplicationContext applicationContext){
Tomcat tomcat = new Tomcat();
Server server = tomcat.getServer();
Service service = server.findService("Tomcat");
Connector connector = new Connector();
connector.setPort(8081);
Engine engine = new StandardEngine();
engine.setDefaultHost("localhost");
Host host = new StandardHost();
host.setName("localhost");
String contextPath = "";
Context context = new StandardContext();
context.setPath(contextPath);
context.addLifecycleListener(new Tomcat.FixContextListener());
host.addChild(context);
engine.addChild(host);
service.setContainer(engine);
service.addConnector(connector);
tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet(applicationContext));
context.addServletMappingDecoded("/*", "dispatcher");
try {
tomcat.start();
} catch (LifecycleException e) {
e.printStackTrace();
}
}startTomcat方法就是啟動Tomcat,需要傳遞一個容器,然后綁定8081端口,在瀏覽器中我們就可以通過“localhost:8081/test”來訪問。
總結
開篇簡單模擬一下SpringBoot的過程,后期逐步來分析一下SpringBoot中的相關源碼。其中大量運用Spring的相關知識。
到此這篇關于簡易版SpringBoot自定義模擬實現(xiàn)的文章就介紹到這了,更多相關模擬實現(xiàn)簡易版SpringBoot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot實現(xiàn)前后端分離excel下載
這篇文章主要介紹了Springboot實現(xiàn)前后端分離excel下載,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring Boot集成LangChain來實現(xiàn)Rag應用的問題小結
檢索增強生成(RAG)是一種優(yōu)化大型語言模型(LLM)輸出的技術,通過引用權威知識庫以增強模型的準確性和相關性,RAG允許LLM在不重新訓練的情況下訪問特定領域的知識,提高了其在各種應用中的實用性和信任度,感興趣的朋友跟隨小編一起看看吧2024-09-09
Springboot內(nèi)置tomcat配置虛擬路徑過程解析
這篇文章主要介紹了Springboot內(nèi)置tomcat配置虛擬路徑過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
SSM框架中entity mapper dao service controll
這篇文章主要介紹了SSM框架中entity mapper dao service controller層的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11

