[運行程序]
三種方式啓動項目 [more]
首發於慕課網 , 歡迎關注Github獲取更多內容)java
File
=> New
=> Project
,能夠看到下圖彈出建立窗口,左側默認指向Spring Initializr,右側Choose Initializr Service Url 默認指向 start.spring.io/ ,這是Spring官方提供的,在這裏也能夠建立工程項目。Next
進入下一步,Group: 本身能夠根據本身的喜好命名,本身的名字等均可以;Name:咱們這裏設置爲user;Type:選擇Maven;更多參數設置參考如下圖片示例Next
進入下一步,能夠看到不少Spring的組件供咱們選擇,這裏只選擇Web。Next
進入下步,選擇項目的存儲位置,點擊Finish完成整個工程的構建經過以上步驟完成了項目的建立,下面讓咱們來看下基本的項目結構:git
├── src 業務代碼目錄 ├── main ├── java 程序入口 ... ├── resources 資源配置文件 ... ├── test 單元測試目錄 ├── ├── pom.xml
pom.xml
github
...
<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>
...
複製代碼
建立 HelloControllerl
類,內容以下web
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!!!";
}
}
複製代碼
啓動有多種方式,讓咱們分別看下spring
Run 'UserApplication'
mvn spring-boot:run
mvn install
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 user-0.0.1-SNAPSHOT.jar
打開瀏覽器訪問http://localhost:8080/hello
,能夠看到頁面輸出Hello SpringBoot!!!
瀏覽器
源碼地址 https://github.com/Q-Angelo/SpringBoot-WebApi/tree/master/chapter1/chapter1-1tomcat
SpringBoot默認使用 application.properties
文件,位於/src/main/resources
目錄下,項目的默認啓動端口是8080,下面對此進行修改springboot
application.propertiesapp
server.port=8081
server.servlet.context-path=/user
複製代碼
還可使用.yml
文件寫,優勢在於更簡潔,推薦此格式maven
刪除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)
複製代碼
在瀏覽器運行此次須要加上咱們的前綴進行訪問 http://localhost:8081/user/hello
項目開發中一般還會須要自定義一些配置文件,格式和上面同樣,讓咱們來設置一些訪問該網站的用戶信息
各參數之間也可相互引用,例以下面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
源碼地址 https://github.com/Q-Angelo/SpringBoot-WebApi/tree/master/chapter1/chapter1-2
一個項目在開發中,至少會有兩個環境:開發環境、生產環境分別來管理數據連接地址,接口請求地址等,那麼對於這種多環境配置咱們該怎麼操做呢?
SpringBoot中多環境配置須要知足 application-{profile}.yml
格式,例如咱們本次實例中即將要介紹的:
application-dev.yml
:開發環境server:
port: 8080
servlet:
context-path: /user
user:
nickName: 張三
age: 18
info: 我今年${user.age},目前訪問的是dev環境。
複製代碼
application-pro.yml
:生產環境server:
port: 8081
servlet:
context-path: /user
user:
nickName: 李四
age: 19
info: 我今年${user.age},目前訪問的是pro環境。
複製代碼
至於哪一個文件會被加載,須要對spring.profiles.active
屬性進行設置。 修改application.yml
文件,會默認加載application-dev.yml
配置文件
spring:
profiles:
active: dev
複製代碼
經過java -jar
的方式啓動
進入項目根目錄,執行命令進行編譯 mvn install
開啓了兩個終端分別執行命令:
開啓dev環境 java -jar target/user-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
開啓pro環境java -jar target/user-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro
如下爲兩個終端的啓動信息,能夠看到分別開啓了8080端口、8081端口
瀏覽器端一樣開啓兩個窗口分別執行:
分別返回不一樣環境對應的配置信息,
經過以上實例,能夠總結出如下3點:
application-{profile}.yml
配置不一樣環境的內容java -jar target/user-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro
這種方式激活當前須要運行的環境信息源碼地址 https://github.com/Q-Angelo/SpringBoot-WebApi/tree/master/chapter1/chapter1-3
做者:五月君
連接:www.imooc.com/article/255…
來源:慕課網
Github: Spring Boot實戰系列