SpringBoot學習筆記(第三課時:項目配置)
目錄
概要
- 註解的使用:
@Value
、@Component
和@ConfigurationProperties
- 多環境的配置(開發和生產)
配置一般都寫在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,我們不動代碼(先停止運行),然後進到項目的根目錄下
- 使用
mvn clean package
打包 - 使用
java -jar -Dspring.profiles.active=prod target/xxx.jar
命令啓動生產模式,java -jar -Dspring.profiles.active=dev target/xxx.jar
命令啓動開發模式(我們前面啓動用的是java -jar target/xxx.jar
)
四、數據庫配置
spring.datasource.url: jdbc:mysql://127.0.0.1:3306
spring.datasource.username: root
spring.datasource.password: 123456
spring.datasource.driver-class-name: com.mysql.jdbc