zoukankan      html  css  js  c++  java
  • Spring Boot Sample 004之spring-boot-configuration-yaml

    一、环境

    • Idea 2020.1
    • JDK 1.8
    • maven

    二、目的

    通过yaml文件配置spring boot 属性文件。

    三、步骤

    3.1、点击File -> New Project -> Spring Initializer,点击next

    3.2、在对应地方修改自己的项目信息

    3.3、选择Web依赖,选中Spring Web。可以选择Spring Boot版本,本次默认为2.2.6,点击Next

    3.4、编辑工程名和项目路径,确定后点击Finish完成

    3.5、项目结构

    四、添加测试方法

    4.1、新建UserController实体类

    package org.ouyushan.springboot.configuration.properties.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.HashMap;
    
    /**
     * @Description:
     * @Author: ouyushan
     * @Email: ouyushan@hotmail.com
     * @Date: 2020/4/27 13:34
     */
    
    @RestController
    @RequestMapping("/api/user")
    public class UserController {
    
        @Value(value = "${ouyushan.secret}")
        private String secret;
    
        @Value(value = "${ouyushan.number}")
        private int number;
    
        @Value(value = "${ouyushan.desc}")
        private String desc;
    
    
        @GetMapping("/")
        public String hello() {
            return "Hello World Based On Spring-Boot";
        }
    
        // @RequestParam 简单类型的绑定
        @GetMapping("/getUser")
        public HashMap<String, Object> getUser(@RequestParam String username) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("title", "hello world");
            map.put("username", username);
            map.put("secret",secret);
            map.put("number",number);
            map.put("desc",desc);
            return map;
        }
    }

    4.2、配置默认application.yaml

    在resources/application.yaml中配置以下信息:
    ouyushan:
      secret: ${random.value}
      number: ${random.int}
      name: ouyushan.org
      desc: ${ouyushan.name} is a domain name
    
    server:
      port: 7070
    
    spring:
      jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: Asia/Chongqing
    启动项目,访问
    返回:
    {
        "number":1410493656,
        "secret":"f99e177aeb5370bec930ec7159bb5df7",
        "title":"hello world",
        "username":"spring",
        "desc":"ouyushan.org is a domain name"
    }
     

    4.3、配置resources/config/application.yaml

    ouyushan:
      secret: ${random.value}
      number: ${random.int}
      name: ouyushan.org
      desc: config ${ouyushan.name} is a domain name
    
    server:
      port: 9090
    
    spring:
      jackson:
      date-format: yyyy-MM-dd HH:mm:ss
      time-zone: Asia/Chongqing

    启动项目,访问

    返回:
    {
        "number":-1333749861,
        "secret":"9e0107e2d0d1b91591a18e8165532201",
        "title":"hello world",
        "username":"spring",
        "desc":"config ouyushan.org is a domain name"
    }
    通过对比请求地址和返回结果,发现resources/config/application.yaml中的配置内容已覆盖resources/application.yaml

    五、知识点

    yaml文件优先级
    
    1. 当前目录下的一个/config子目录
    2. 当前目录
    3. 一个classpath下的/config包
    4. classpath根路径(root)
     
     yaml中: 后面有空格
     
    可通过@Value取值
    
        @Value(value = "${ouyushan.secret}")
        private String secret;
    
    
    **@ConfigurationProperties(prefix = "sample")**
    
    读取外部配置文件中sample 开始的属性,自动映射到类中的字段,并覆盖代码中的值。
     
    当同名文件存在两种格式时,先加载yaml文件再加载properties文件,会导致properties的值会覆盖yaml配置的值
  • 相关阅读:
    个人作业——软件工程实践总结作业
    团队作业——Beta冲刺5
    Beta冲刺博客集合贴
    团队作业——Beta冲刺4
    团队作业——Beta冲刺3
    团队作业——Beta冲刺2
    团队作业——Beta冲刺1
    Alpha事后诸葛亮(阳光普照队)
    软件产品案例分析
    个人作业——软件工程实践总结作业
  • 原文地址:https://www.cnblogs.com/ouyushan/p/13023906.html
Copyright © 2011-2022 走看看