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:3306spring.datasource.username: rootspring.datasource.password: 123456spring.datasource.driver-class-name: com.mysql.jdbc

本文深入讲解SpringBoot项目配置,包括端口与路径调整、多环境配置、数据库连接配置及使用注解实现配置注入,适用于初学者掌握核心配置技巧。
1万+

被折叠的 条评论
为什么被折叠?



