SpringBoot學習筆記(第三課時:項目配置)

SpringBoot學習筆記(第三課時:項目配置)

目錄

概要

  1. 註解的使用:@Value@Component@ConfigurationProperties
  2. 多環境的配置(開發和生產)

配置一般都寫在src/resources/application.propertise文件裏面編寫

一、基本配置

方法1:
修改默認端口:server.port=8081
修改context-path: server.servlet.context-path=/luckymoney,前面我們http://localhost:8081/hello訪問,現在我們要這麼訪問:http://localhost:8081/luckymoney/hello

server.port=8081
server.servlet.context-path=/luckymoney

方法2:
以上配置我們還可以用一下方法進行配置
我們新建個文件,在與application.propertise同一目錄下新建文件application.yml(YAML類型)
在這裏插入圖片描述
然後我們再在這個文件裏面編寫(注意:要把原來的application.propertise文件刪除)。application.yml配置內容

server:
  port: 8081
  servlet:
    context-path: /luckymoney

推薦使用方法2

二、擴展配置

application.yml

# 在配置裏面設置常量
minMoney: 1
# 在配置裏面使用配置
description: 最少要發${minMoney}

在程序HelloController裏面使用

package com.example.luckymoney;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

// 註解
@RestController
public class HelloController {
    // 獲取配置 金額用BigDecimal類型
    @Value("${minMoney}")
    private BigDecimal minMoney;

    @Value("${description}")
    private String description;

    @GetMapping("/hello")
    public String say() {
        return "minMoney: " + minMoney + "," + description;
    }
}

結果:
在這裏插入圖片描述
我們發現,如果有多個配置,用@Value的方法就有點累贅,下面我們做一些改動(對象):
我們創建一個對象叫做LimitConfig,然後寫一下字段,並給他Getter and Setter方法,然後使用 @Component@ConfigurationProperties(prefix = "limit")把配置注入進來。
1.、application.yml

limit:
  # 注意是兩個空格
  minMoney: 1
  maxMoney: 999
  # 在配置裏面使用配置
  description: 最少要發${limit.minMoney}元,最多隻能發${limit.maxMoney}

2、 LimitConfig

package com.example.luckymoney;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
// 這兩個註解必須要
@Component
@ConfigurationProperties(prefix = "limit")
public class LimitConfig {
    private BigDecimal minMoney;
    private BigDecimal maxMoney;
    private String description;
    public BigDecimal getMinMoney() {
        return minMoney;
    }
    public void setMinMoney(BigDecimal minMoney) {
        this.minMoney = minMoney;
    }
    public BigDecimal getMaxMoney() {
        return maxMoney;
    }
    public void setMaxMoney(BigDecimal maxMoney) {
        this.maxMoney = maxMoney;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

3、HelloController

package com.example.luckymoney;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

// 註解
@RestController
public class HelloController {

    @Autowired
    private LimitConfig limitConfig;

    @GetMapping("/hello")
    public String say() {
        return "說明:" + limitConfig.getDescription();
    }
}

4、結果
在這裏插入圖片描述

三、開發和生產模式配置

由於測試中不可能就支付1元的最小紅包,我們需要在開發環境中使用0.01元來測試,所以我們需要寫一個開發環境的配置,那麼劃出兩個環境:一個開發環境,一個生產環境。
我們需要複製兩份,如下:
在這裏插入圖片描述
application-dev.yml
在這裏插入圖片描述
application-prod.yml
在這裏插入圖片描述
然後我們再將application.yml裏面的清空,作如下配置
application.yml

spring:
  profiles:
    active: dev

我們再次啓動,用的就是開發模式(dev)
在這裏插入圖片描述
以此類推,prod爲生產模式。
那麼我們是不是每次發佈修改是不是都要修改dev和prod呢?
no,我們不動代碼(先停止運行),然後進到項目的根目錄下

  1. 使用mvn clean package打包
  2. 使用java -jar -Dspring.profiles.active=prod target/xxx.jar命令啓動生產模式,java -jar -Dspring.profiles.active=dev target/xxx.jar命令啓動開發模式(我們前面啓動用的是java -jar target/xxx.jar

四、數據庫配置

  1. spring.datasource.url: jdbc:mysql://127.0.0.1:3306
  2. spring.datasource.username: root
  3. spring.datasource.password: 123456
  4. spring.datasource.driver-class-name: com.mysql.jdbc
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章