[spring]SpringBoot配置文件

一. 配置文件作用

硬编码是将数据直接嵌⼊到程序或其他可执⾏对象的源代码中, 也就是我们常说的"代码写死".
⽐如⼿机字体⼤⼩
如果采⽤硬编码的⽅式, 就直接在程序中指定字体⼤⼩, 所有的⽤⼾使⽤的都是同⼀个字体⼤⼩
但是不同的⽤⼾有不同的偏好, 我们可以把⼿机字体的⼤⼩放在配置⽂件中, 当程序启动时, 读取配置, 以⽤⼾设置的字体⼤⼩来显⽰

SpringBoot配置文件

SpringBoot⽀持并定义了配置⽂件的格式, 也在另⼀个层⾯达到了规范其他框架集成到SpringBoot的
⽬的
很多项⽬或者框架的配置信息也放在配置⽂件中, ⽐如:
• 项⽬的启动端⼝
• 数据库的连接信息(包含⽤⼾名和密码的设置)
• 第三⽅系统的调⽤密钥等信息
• ⽤于发现和定位问题的普通⽇志和异常⽇志等
在这里插入图片描述

二. 配置文件格式

Spring Boot 配置⽂件有以下三种:
• application.properties
• application.yml
• application.yaml
(yml是yaml的简写, 实际开发中出现的频率最高)

  1. 理论上讲 .properties 和 .yml 可以并存在于⼀个项⽬中,当 .properties 和 .yml并存时,两个配置都会加载. 如果配置⽂件内容有冲突, 则以 .properties 为主, 也就是.properties 优先级更⾼.
  2. 虽然理论上来讲 .properties 可以和 .yml 共存,但实际的业务当中,我们通常会采取⼀种统⼀的配置⽂件格式,这样可以更好的维护(降低故障率)

properties配置文件

properties 是以键值的形式配置的,key 和 value 之间是以"="连接的
在这里插入图片描述
读取properties: 使用@value(“${ }”)
在这里插入图片描述
在这里插入图片描述

yml配置文件

yml 是 YAML 是缩写,它的全称 Yet Another Markup Language 翻译成中⽂就是“另⼀种标记语⾔.
yml 是树形结构的配置⽂件,它的基础语法是"key: value".
key 和 value 之间使⽤英⽂冒号加空格的⽅式组成,空格不可省略
在这里插入图片描述

读取yml: 使用@value(“${ }”)
在这里插入图片描述

yml配置不同的数据类型
~表示null
‘’ 或"" 表示空字符串

字符串默认不⽤加上单引号或者双引号,如果加英⽂的单双引号可以表⽰特殊的含义
在这里插入图片描述
在这里插入图片描述
(\n本身表示为换行)
• 字符串默认不⽤加上单引号或者双引号。
• 单引号会转义特殊字符,使其失去特殊功能, 始终是⼀个普通的字符串.
• 双引号不会转义字符串⾥⾯的特殊字符, 特殊字符会表⽰本⾝的含义.

配置对象
使用@ConfigurationProperties(prefix = " …"), prefix = 可省略
表示被标记的这个对象要从配置中获取
在这里插入图片描述
想要拿到这个类使用, 还是用@Autowired
在这里插入图片描述
配置集合
在这里插入图片描述
在这里插入图片描述
配置Map
在这里插入图片描述
在这里插入图片描述

yml优缺点

在这里插入图片描述
在这里插入图片描述

三. 应用(验证码)

工具:hutool
controller:

@RequestMapping("/captcha")
@Controller
public class captchaController {
    @Autowired
    private CaptchaProperties captchaProperties;

    private final static long VALID_MILLIS_TIME = 60 * 1000;

    @RequestMapping("/getCaptcha")
    public void getCaptcha(HttpSession session, HttpServletResponse response){
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(captchaProperties.getWidth(), captchaProperties.getHeight());
        response.setContentType("image/jpeg");
        response.setHeader("Pragma", "No-cache");
        try {
            lineCaptcha.write(response.getOutputStream());
            session.setAttribute(captchaProperties.getSession().getKey(), lineCaptcha.getCode());
            session.setAttribute(captchaProperties.getSession().getDate(), new Date());
            System.out.println("生成验证码" + lineCaptcha.getCode());
            response.getOutputStream().close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @ResponseBody
    @RequestMapping("/check")
    public Boolean checkHomeCaptcha(String captcha, HttpSession session){
        if(!StringUtils.hasLength(captcha)){
            return false;
        }
        String savedCaptcha = (String)session.getAttribute(captchaProperties.getSession().getKey());
        Date sessionDate = (Date)session.getAttribute(captchaProperties.getSession().getDate());
        if(captcha.equalsIgnoreCase(savedCaptcha)){
            if(sessionDate == null || System.currentTimeMillis() - sessionDate.getTime() < VALID_MILLIS_TIME){
                return true;
            }
            return false;
        }
        return false;
    }
}

CaptchaProperties:

@Data
@Component
@ConfigurationProperties("captcha")

public class CaptchaProperties {
    private Integer width;
    private Integer height;
    private Session session;

    @Data
    public static class Session{
        private String  key;
        private String date;
    }
}

application.yml:

captcha:
  width: 100
  height: 40
  session:
    key: CAPTCHA_SESSION_KEY
    date: CAPTCHA_SESSION_DATE
    

pom.xml:

 		<dependency>
 		<groupId>cn.hutool</groupId>
 		<artifactId>hutool-all</artifactId>
 		<version>5.8.26</version>
 	</dependency>
 	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值