簡易版SpringBoot自定義模擬實(shí)現(xiàn)
SpringBoot作為目前最流行的框架之一,同時(shí)是每個(gè)程序員必須掌握的知識,其提供了豐富的功能模塊和開箱即用的特性,極大地提高了開發(fā)效率和降低了學(xué)習(xí)成本,使得開發(fā)人員能夠更專注于業(yè)務(wù)邏輯的實(shí)現(xiàn),而無需過多關(guān)注底層框架的配置和集成。本文模擬實(shí)現(xiàn)簡易版SpringBoot。
模塊創(chuàng)建
創(chuàng)建一個(gè)Springboot源碼模塊,主要用來實(shí)現(xiàn)SpringBoot的核心編程邏輯,類似導(dǎo)入SpringBoot依賴。
創(chuàng)建一個(gè)應(yīng)用模塊Demo,用來實(shí)現(xiàn)業(yè)務(wù)邏輯測試我們自己編寫好的Springboot代碼。
依賴導(dǎo)入
由于SpringBoot是依賴于Spring的也依賴SpringMVC,所以我們也得依賴Spring和SpringMVC,導(dǎo)入Spring與SpringMVC的相關(guān)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模塊就可以類似平常一樣,隨便寫需要什么導(dǎo)入什么,但是得依賴于我們自己寫的SpringBoot模塊。
<dependencies> <dependency> <groupId>com.simulate.example</groupId> <artifactId>springboot</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
代碼編寫
Demo模塊的代碼直接就正常編寫邏輯,定義一個(gè)Controller,Service一個(gè)接口請求方法執(zhí)行“/test”。
SpringBoot模塊,效仿真正的SpringBoot項(xiàng)目在項(xiàng)目啟動類里面存在一個(gè)注解,傳入配置類,然后調(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){ } }
首先我們需要去定義一個(gè)核心的注解類和一個(gè)啟動類DemoSpringApplication。
定義完這兩個(gè)類此時(shí)我們就可以去編寫Demo業(yè)務(wù)的啟動類,之前是表示@SpringBootApplication,現(xiàn)在通過我們自定義的注解來實(shí)現(xiàn)。
@DemoSpringBootApplication public class MyApplication { public static void main(String[] args) { MySpringApplication.run(MyApplication.class); } }
實(shí)現(xiàn)run方法邏輯
我想著當(dāng)run方法結(jié)束后,我們就可以在瀏覽器里面訪問我們之前定義好的test路徑,那么run方法必定會去啟動Tomcat服務(wù)才能夠在瀏覽器里面訪問,所在方法里面必須去啟動一個(gè)Tomcat服務(wù)。
同時(shí)我們需要掃描得到Spring的相關(guān)類,同時(shí)還得利用Springmvc去進(jìn)行相關(guān)操作,將DispatcherServlet加入到Tomcat中。
在run方法里面需要實(shí)現(xiàn)邏輯:創(chuàng)建一個(gè)Spring容器,創(chuàng)建Tomcat對象,創(chuàng)建DispatcherServlet對象并且和前面創(chuàng)建出來的Spring容器進(jìn)行綁定將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就會去掃描類上的相關(guān)注解,這時(shí)候就會掃描到我們自己寫好的@DemoSpringBootApplication注解,然后該注解內(nèi)存存在@ComponentScan注解等都會一并去掃描實(shí)現(xiàn),ComponentScan就是去掃描路徑找到bean對象,如果沒有指定路徑默認(rèn)就是配置類所在包路徑,就會將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,需要傳遞一個(gè)容器,然后綁定8081端口,在瀏覽器中我們就可以通過“localhost:8081/test”來訪問。
總結(jié)
開篇簡單模擬一下SpringBoot的過程,后期逐步來分析一下SpringBoot中的相關(guān)源碼。其中大量運(yùn)用Spring的相關(guān)知識。
到此這篇關(guān)于簡易版SpringBoot自定義模擬實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)模擬實(shí)現(xiàn)簡易版SpringBoot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot實(shí)現(xiàn)前后端分離excel下載
這篇文章主要介紹了Springboot實(shí)現(xiàn)前后端分離excel下載,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Spring Boot集成LangChain來實(shí)現(xiàn)Rag應(yīng)用的問題小結(jié)
檢索增強(qiáng)生成(RAG)是一種優(yōu)化大型語言模型(LLM)輸出的技術(shù),通過引用權(quán)威知識庫以增強(qiáng)模型的準(zhǔn)確性和相關(guān)性,RAG允許LLM在不重新訓(xùn)練的情況下訪問特定領(lǐng)域的知識,提高了其在各種應(yīng)用中的實(shí)用性和信任度,感興趣的朋友跟隨小編一起看看吧2024-09-09Springboot內(nèi)置tomcat配置虛擬路徑過程解析
這篇文章主要介紹了Springboot內(nèi)置tomcat配置虛擬路徑過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04SSM框架中entity mapper dao service controll
這篇文章主要介紹了SSM框架中entity mapper dao service controller層的使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11詳解MyBatis中主鍵回填的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了詳解MyBatis中主鍵回填的兩種實(shí)現(xiàn)方式,主鍵回填其實(shí)是一個(gè)非常常見的需求,特別是在數(shù)據(jù)添加的過程中,我們經(jīng)常需要添加完數(shù)據(jù)之后,需要獲取剛剛添加的數(shù)據(jù) id,有興趣的可以參考一下2019-04-04