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