前言
spring在java中作為一個(gè)框架,非常收到歡迎,典型的xml配置也讓整個(gè)系統(tǒng)的結(jié)構(gòu)比較清晰,兼容他的各種sdk,相關(guān)的組件等也非常豐富。springboot也是基于spring的一套java上的框架,他自己針對(duì)一些內(nèi)容做一些操作和組裝,而且目前在各大公司大量的使用。
本人覺得spring比較清晰,xml也能把整個(gè)app進(jìn)行貫穿,了解系統(tǒng)中都有什么。但是springboot大量的注解實(shí)例化,讓整個(gè)開發(fā)變得模糊和不清晰,個(gè)人來說目前對(duì)springboot并沒有多好的“印象”。
那就以目前一個(gè)局外人的角度去開始慢慢接觸springboot都有什么?
廢話少說,創(chuàng)建一個(gè)試試
1. 我們先創(chuàng)建一個(gè)普通pom項(xiàng)目
新建一個(gè)類ApplicationMain,同時(shí)寫main方法,簡(jiǎn)單的輸出一行文字,運(yùn)行后,控制臺(tái)輸出了文字后程序退出。
2. 引入springboot 的pom引用
添加如下的引用:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
/* 請(qǐng)注意上面這一行,在很多網(wǎng)上的文檔中都是“spring-boot-starter-web ”名字的包
web方式啟動(dòng)是整個(gè)服務(wù)對(duì)外提供http接口的方式并默認(rèn)以服務(wù)器的方式啟動(dòng),
我們這里使用spring-boot-starter主要是讓程序像普通程序一樣跑完就結(jié)束的流程,方便演示
當(dāng)然如果服務(wù)不使用http或相關(guān)內(nèi)容也可以使用該包 */
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
注意上面代碼中的注釋,絕大多數(shù)網(wǎng)上介紹的內(nèi)容都是默認(rèn)使用web。
這個(gè)時(shí)候,我們啟動(dòng)程序,發(fā)現(xiàn)還是一樣的啟動(dòng),沒有任何區(qū)別??刂婆_(tái)輸出也是一模一樣。這個(gè)時(shí)候其實(shí)并沒有使用springboot的相關(guān)內(nèi)容。
3. 開始代碼初始化springboot。
在main函數(shù)中國(guó)呢添加代碼
public static void main(String[] args) {
System.out.println("main starting...");
SpringApplication application = new SpringApplication(ApplicationMain.class);
application.run(args);
System.out.println("main started!!!");
}
運(yùn)行,我們看到結(jié)果:
main starting...
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.4.RELEASE)
2019-10-30 19:38:02.992 INFO 4770 --- [ main] cn.miludeer.deer.ApplicationMain : Starting ApplicationMain on MacBook-Pro-9.local with PID 4770 (/Users/mac/code/cnmiludeer/target/classes started by mac in /Users/mac/code/cnmiludeer)
2019-10-30 19:38:02.996 INFO 4770 --- [ main] cn.miludeer.deer.ApplicationMain : No active profile set, falling back to default profiles: default
2019-10-30 19:38:03.019 INFO 4770 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7ff2a664: startup date [Wed Oct 30 19:38:03 CST 2019]; root of context hierarchy
2019-10-30 19:38:03.384 INFO 4770 --- [ main] cn.miludeer.deer.ApplicationMain : Started ApplicationMain in 0.771 seconds (JVM running for 1.578)
main started!!!
2019-10-30 19:38:03.388 INFO 4770 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7ff2a664: startup date [Wed Oct 30 19:38:03 CST 2019]; root of context hierarchy
Process finished with exit code 0
首先看到的是main中打印的“starting... ”, 然后就是Springboot啟動(dòng)的標(biāo)示和版本號(hào),接著是一些初始化,后續(xù)程序運(yùn)行結(jié)束,打印一些收尾的工作。最后結(jié)束。
到這里為止我們就開始使用了springboot,main函數(shù)自身調(diào)用SpringApplication進(jìn)行初始化并run既是進(jìn)入了啟動(dòng)springboot的流程。
總結(jié)
- 我們此次實(shí)驗(yàn)的很簡(jiǎn)單,就是開始嘗試如何加載使用springboot;
- 進(jìn)入SpringApplication.run() 之后就是開始了springboot的內(nèi)部。內(nèi)部初始化我們需要的實(shí)例給spring管理,如何配置等等。
下一節(jié)我們看下基本的如何配置和使用以及從中我們可以看到的一些內(nèi)容。