目前RocketMQ總算可以告一段落了,在學習的過程中是否遇到什么難點,前面的知識點是否還依稀記得,后面有時間還是需要好好復習一遍,不過總的來說還是得會使用,學習源碼是為了能夠讓你更加了解該技術,在碰到問題的同時能夠更加快速的發現和解決問題。
接下來我們來分析一個Springboot的相關知識,讓我們一起開啟新篇章吧!!!
SpringBoot作為目前最流行的框架之一,同時是每個程序員必須掌握的知識,其提供了豐富的功能模塊和開箱即用的特性,極大地提高了開發效率和降低了學習成本,使得開發人員能夠更專注于業務邏輯的實現,而無需過多關注底層框架的配置和集成。
創建一個Springboot源碼模塊,主要用來實現SpringBoot的核心編程邏輯,類似導入SpringBoot依賴。
創建一個應用模塊Demo,用來實現業務邏輯測試我們自己編寫好的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一個接口請求方法執行“/test”。
SpringBoot模塊,效仿真正的SpringBoot項目在項目啟動類里面存在一個注解,傳入配置類,然后調用run方法即可。
/** * @author dream */@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Configuration@ComponentScanpublic @interface DemoSpringBootApplication {}public class MySpringApplication { public static void run(Class clazz){ }}
首先我們需要去定義一個核心的注解類和一個啟動類DemoSpringApplication。
定義完這兩個類此時我們就可以去編寫Demo業務的啟動類,之前是表示@SpringBootApplication,現在通過我們自定義的注解來實現。
@DemoSpringBootApplicationpublic class MyApplication { public static void main(String[] args) { MySpringApplication.run(MyApplication.class); }}
我想著當run方法結束后,我們就可以在瀏覽器里面訪問我們之前定義好的test路徑,那么run方法必定會去啟動Tomcat服務才能夠在瀏覽器里面訪問,所在方法里面必須去啟動一個Tomcat服務。
同時我們需要掃描得到Spring的相關類,同時還得利用Springmvc去進行相關操作,將DispatcherServlet加入到Tomcat中。
在run方法里面需要實現邏輯:創建一個Spring容器,創建Tomcat對象,創建DispatcherServlet對象并且和前面創建出來的Spring容器進行綁定將DispatcherServlet添加到Tomcat中,最后啟動Tomcat。
public static void run(Class clazz) { AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); applicationContext.register(clazz); applicationContext.refresh(); }
創建AnnotationConfigWebApplicationContext容易傳入class類就表示該clazz為配置類,Spring就會去掃描類上的相關注解,這時候就會掃描到我們自己寫好的@DemoSpringBootApplication注解,然后該注解內存存在@ComponentScan注解等都會一并去掃描實現,ComponentScan就是去掃描路徑找到bean對象,如果沒有指定路徑默認就是配置類所在包路徑,就會將Demo的Controller類掃描到Spring中,并將訪問地址掃描到其中。
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的相關知識,如果有不理解的地方可以提出來或者去翻閱前面的知識點。
本文鏈接:http://www.www897cc.com/showinfo-26-55280-0.html簡易版的SpringBoot是如何實現的!!!
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 我們一起聊聊如何做程序的性能優化
下一篇: 我們一起聊聊如何做程序的性能優化