SpringBoot的主要目的是簡(jiǎn)化配置文件仇奶,通過(guò)少量配置即可運(yùn)行Java程序,其強(qiáng)大的自動(dòng)配置功能幫助開發(fā)者輕松實(shí)現(xiàn)配置裝配比驻,通過(guò)引入SpringBoot的starter就能實(shí)現(xiàn)想要的功能该溯,不需要額外的配置。
目前SpringBoot工程有三種搭建方式:
通過(guò)Spring Initializr創(chuàng)建
通過(guò)IDEA創(chuàng)建工程
手動(dòng)創(chuàng)建工程
1. 通過(guò)Spring Initializr創(chuàng)建
Spring團(tuán)隊(duì)提供一個(gè)非常方便的網(wǎng)頁(yè)用于生成SpringBoot工程别惦,打開瀏覽器進(jìn)入 Spring Initializr:
工程生成參數(shù)列表:
Project: 工程類型(支持Maven和Gradle構(gòu)建工具)
Language:
工程主要語(yǔ)言根據(jù)需要可選擇Java狈茉、Kotlin、Groovy
SpringBoot:
SpringBoot版本
ProjectMatedata:
有Group和Artifact等配置
Dependencies:
工程依賴
參數(shù)設(shè)置完成后點(diǎn)擊Generate下載工程掸掸,完成后使用IDEA導(dǎo)入工程氯庆,打開工程同步即可運(yùn)行蹭秋。
2. 通過(guò)IDEA創(chuàng)建工程(筆者推薦)
筆者較為推薦這種方式,因?yàn)閮蓚€(gè)字?簡(jiǎn)單
較新的IDEA版本都內(nèi)置創(chuàng)建SpringBoot工程插件堤撵,其創(chuàng)建原理也是使用的 Spring Initializr 來(lái)創(chuàng)建工程仁讨,創(chuàng)建流程下如:
打開IDEA開發(fā)工具
選擇file -> new -> project菜單
在新的對(duì)話框中選擇Spring Initializr
點(diǎn)擊Next即可創(chuàng)建SpringBoot工程
最后main方法啟動(dòng)應(yīng)用程序:
@SpringBootApplication
public class SbhelloApplication {
public static void main(String[] args) {
SpringApplication.run(SbhelloApplication.class, args);
? }
}
3.手動(dòng)創(chuàng)建工程
除了以上兩種方式外,還可以通過(guò)手動(dòng)創(chuàng)建的方式創(chuàng)建SpringBoot工程实昨,通過(guò)IDEA創(chuàng)建一個(gè)空的Maven工程洞豁,然后指定SpringBoot的依賴就,基本流程如下:
打開IDEA開發(fā)工具
選擇file -> new -> project菜單
在新的對(duì)話框中選擇Mavenn
點(diǎn)擊Next根據(jù)提示完成項(xiàng)目創(chuàng)建
工程創(chuàng)建完成后荒给,打開pom.xml文件丈挟,設(shè)置pom.xml的parent配置:
org.springframework.bootspring-boot-starter-parent2.2.0.RELEASE
添加SpringBootMaven打包插件:
org.springframework.bootspring-boot-maven-plugin
添加main方法啟動(dòng)應(yīng)用程序:
@SpringBootApplicationpublicclassSbhelloApplication{publicstaticvoidmain(String[] args){? ? SpringApplication.run(SbhelloApplication.class, args);? }}
完整pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>4.0.0com.zlsbhello0.0.1-SNAPSHOTsbhelloDemo project for Spring Bootorg.springframework.bootspring-boot-starter-parent2.3.3.RELEASE<!-- lookup parent from repository -->1.8org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engine<!--將tomcat打包成可執(zhí)行的jar-->org.springframework.bootspring-boot-maven-plugin
設(shè)置parent和插件后,就可以使用SpringBoot了志电。