Spring Boot實戰系列(1)項目構建

快速導航

首發於慕課網 , 歡迎關注Github獲取更多內容)java

IntelliJ IDEA 中的Spring Initializr快速構建SpringBoot工程

intellig編輯器建立

  • 菜單欄中選擇 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.xmlgithub

  • spring-boot-starter-web: Web項目模塊依賴
  • spring-boot-starter-test: 測試模塊依賴
  • spring-boot-maven-plugin: Maven構建項目插件
...
    <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-程序

建立 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
    • 進到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

打開瀏覽器訪問http://localhost:8080/hello,能夠看到頁面輸出Hello SpringBoot!!!瀏覽器

源碼地址 https://github.com/Q-Angelo/SpringBoot-WebApi/tree/master/chapter1/chapter1-1tomcat

項目屬性配置

後綴properties文件配置

SpringBoot默認使用 application.properties文件,位於/src/main/resources目錄下,項目的默認啓動端口是8080,下面對此進行修改springboot

  • server.port:修改端口號
  • server.context-path:設置url前綴 SpringBoot2.0版本如下采用此方法
  • server.servlet.context-path:設置url前綴SpringBoot2.0版本以上使用

application.propertiesapp

server.port=8081
server.servlet.context-path=/user
複製代碼

後綴yml文件配置

還可使用.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.yml 用來存放公共配置,設置spring.profiles.active=dev,默認開發環境配置
  • 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實戰系列

相關文章
相關標籤/搜索