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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值