快速導(dǎo)航
- IntelliJ IDEA 中的Spring Initializr快速構(gòu)建SpringBoot工程
-
編寫一個Hello SpringBoot程序
-
[運行程序]三種方式啟動項目[more]
-
- 項目屬性配置
IntelliJ IDEA 中的Spring Initializr快速構(gòu)建SpringBoot工程
intellig編輯器創(chuàng)建
- 菜單欄中選擇
File=>New=>Project,可以看到下圖彈出創(chuàng)建窗口,左側(cè)默認(rèn)指向Spring Initializr,右側(cè)Choose Initializr Service Url 默認(rèn)指向 https://start.spring.io/ ,這是Spring官方提供的,在這里也可以創(chuàng)建工程項目。

- 點擊
Next進(jìn)入下一步,Group: 自己可以根據(jù)自己的喜愛命名,自己的名字等都可以;Name:我們這里設(shè)置為user;Type:選擇Maven;更多參數(shù)設(shè)置參考以下圖片示例

- 點擊
Next進(jìn)入下一步,可以看到很多Spring的組件供我們選擇,這里只選擇Web。

- 點擊
Next進(jìn)入下步,選擇項目的存儲位置,點擊Finish完成整個工程的構(gòu)建

通過以上步驟完成了項目的創(chuàng)建,下面讓我們來看下基本的項目結(jié)構(gòu):
<pre>
├── src 業(yè)務(wù)代碼目錄
├── main
├── java 程序入口
...
├── resources 資源配置文件
...
├── test 單元測試目錄
├──
├── pom.xml
</pre>
pom.xml
- spring-boot-starter-web: Web項目模塊依賴
- spring-boot-starter-test: 測試模塊依賴
- spring-boot-maven-plugin: Maven構(gòu)建項目插件
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
...
編寫一個hello-springboot-程序
創(chuàng)建 HelloControllerl 類,內(nèi)容如下
package com.angelo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say() {
return "Hello SpringBoot!!!";
}
}
三種啟動方式
啟動有多種方式,讓我們分別看下
- 方法一:啟動類上,右鍵單機運行
Run 'UserApplication'
[圖片上傳失敗...(image-5f6503-1541685607725)]
- 方法二:進(jìn)到項目根目錄執(zhí)行命令
mvn spring-boot:run - 方法三:
- 先執(zhí)行命令進(jìn)行編譯
mvn install - 進(jìn)到target目錄可以看到有個
user-0.0.1-SNAPSHOT.jar文件
$ cd target $ ls classes maven-archiver test-classes generated-sources maven-status user-0.0.1-SNAPSHOT.jar generated-test-sources surefire-reports user-0.0.1-SNAPSHOT.jar.original- 通過java -jar命令啟動
java -jar user-0.0.1-SNAPSHOT.jar
- 先執(zhí)行命令進(jìn)行編譯
打開瀏覽器訪問http://localhost:8080/hello,可以看到頁面輸出Hello SpringBoot!!!
源碼地址 https://github.com/Q-Angelo/SpringBoot-Course/tree/master/chapter1/chapter1-1
項目屬性配置
后綴properties文件配置
SpringBoot默認(rèn)使用 application.properties文件,位于/src/main/resources目錄下,項目的默認(rèn)啟動端口是8080,下面對此進(jìn)行修改
- server.port:修改端口號
- server.context-path:設(shè)置url前綴 SpringBoot2.0版本以下采用此方法
- server.servlet.context-path:設(shè)置url前綴SpringBoot2.0版本以上使用
application.properties
server.port=8081
server.servlet.context-path=/user
后綴yml文件配置
還可以使用.yml文件寫,優(yōu)點在于更簡潔,推薦此格式
刪除application.properties文件,新建application.yml文件
application.yml
server:
port: 8081
servlet:
context-path: /user
通過以上配置在重啟我們的項目,可以看到以下提示,Tomcat started on port(s): 8081 (http) with context path '/user'
2018-10-21 16:31:51.003 INFO 14696 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path '/user'
2018-10-21 16:31:51.008 INFO 14696 --- [ main] com.angelo.UserApplication : Started UserApplication in 2.999 seconds (JVM running for 4.054)
在瀏覽器運行這次需要加上我們的前綴進(jìn)行訪問 http://localhost:8081/user/hello
[圖片上傳失敗...(image-8f266d-1541685607725)]
自定義屬性配置及參數(shù)間引用
項目開發(fā)中通常還會需要自定義一些配置文件,格式和上面一樣,讓我們來設(shè)置一些訪問該網(wǎng)站的用戶信息
各參數(shù)之間也可相互引用,例如下面info通過${}在括號里引用了user.age
application.yml
server:
port: 8081
servlet:
context-path: /user
user:
nickName: 張三
age: 18
info: 我今年${user.age}。
/src/main/resources目錄下新建UserProperties.java文件
UserProperties.java
package com.angelo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "user") // 獲取前綴是user的配置
public class UserProperties {
private String nickName;
private String info;
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
修改HelloController.java
package com.angelo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
public class HelloController {
@Autowired
private UserProperties userProperties;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say() {
return "我是 " + userProperties.getNickName() + userProperties.getInfo();
}
}
啟動,瀏覽器運行http://localhost:8081/user/hello
[圖片上傳失敗...(image-a5fa2f-1541685607725)]
源碼地址 https://github.com/Q-Angelo/SpringBoot-Course/tree/master/chapter1/chapter1-2
多環(huán)境動態(tài)配置
一個項目在開發(fā)中,至少會有兩個環(huán)境:開發(fā)環(huán)境、生產(chǎn)環(huán)境分別來管理數(shù)據(jù)鏈接地址,接口請求地址等,那么對于這種多環(huán)境配置我們該怎么操作呢?
SpringBoot中多環(huán)境配置需要滿足 application-{profile}.yml格式,例如我們本次實例中即將要介紹的:
-
application-dev.yml:開發(fā)環(huán)境
server:
port: 8080
servlet:
context-path: /user
user:
nickName: 張三
age: 18
info: 我今年${user.age},目前訪問的是dev環(huán)境。
-
application-pro.yml:生產(chǎn)環(huán)境
server:
port: 8081
servlet:
context-path: /user
user:
nickName: 李四
age: 19
info: 我今年${user.age},目前訪問的是pro環(huán)境。
至于哪個文件會被加載,需要對spring.profiles.active屬性進(jìn)行設(shè)置。
修改application.yml文件,會默認(rèn)加載application-dev.yml配置文件
spring:
profiles:
active: dev
通過java -jar的方式啟動
進(jìn)入項目根目錄,執(zhí)行命令進(jìn)行編譯 mvn install
開啟了兩個終端分別執(zhí)行命令:
開啟dev環(huán)境
java -jar target/user-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev開啟pro環(huán)境
java -jar target/user-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro
以下為兩個終端的啟動信息,可以看到分別開啟了8080端口、8081端口
[圖片上傳失敗...(image-38454e-1541685607725)]
瀏覽器端同樣開啟兩個窗口分別執(zhí)行:
分別返回不同環(huán)境對應(yīng)的配置信息,
[圖片上傳失敗...(image-6cf7f8-1541685607725)]
通過以上實例,可以總結(jié)出以下3點:
- application.yml 用來存放公共配置,設(shè)置spring.profiles.active=dev,默認(rèn)開發(fā)環(huán)境配置
-
application-{profile}.yml配置不同環(huán)境的內(nèi)容 - 通過命令行
java -jar target/user-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro這種方式激活當(dāng)前需要運行的環(huán)境信息
源碼地址 https://github.com/Q-Angelo/SpringBoot-Course/tree/master/chapter1/chapter1-3
作者:五月君
鏈接:https://www.imooc.com/article/255895
來源:慕課網(wǎng)
Github: Spring Boot實戰(zhàn)系列