zoukankan      html  css  js  c++  java
  • sprongboot yml文件语法

    SpringBoot默认会加载resource下的配置文件:

    • application*.yml
    • application*.yaml
    • application*.properties

    这也是配置文件的加载顺序,如果某个key有多个配置,则后加载的会覆盖之前加载的配置。

    yml、yaml是同一种文件,后缀写成yml、yaml都可以。一般使用application.yml。

    springboot在不同的环境下有默认的加载文件:

    • application  开发、测试、生产都会加载,公共的
    • application-dev  只在开发环境加载(调试src/main)
    • application-test  只在测试环境加载(调试src/test)
    • application-prod  只在生产环境加载(正式打包部署)

    yml文件语法

    key、value冒号分隔,冒号后面都要加一个空格,加了空格后key会变成橙色,才有效。

    (1)普通字段:

    name: zhangsan

    (2)对象、Map

    对象、Map的配置方式是一样的。

    student: #对象名、Map名
      id: 1  #配置一个属性、一个键值对
      name: chy
      age: 20
      score: 100
    行内写法:
    student: {id:1,name:chy,age:20,score:100}

    (3)数组、List

    city: [beijing,shanghai,guangzhou,shenzhen]
    student: [{name: zhangsan,age: 20},{name: lisi,age: 20}]  #元素可以是对象
    换行写法:
    city:
      - beijinig
      - shanghai
      - guangzhou
      - shenzhen
    student:
      - name: zhangsan
       age: 20
      - name: lisi
       age: 20

    值,不管是key的值,还是数组元素,都不加引号。

    使用yml中的值

    如果是springboot预定义的key,springboot会自动使用它。如果是自定义的key,就需要我们自己来引用。有2种引用方式。

    (1)使用@Value

    name: chy
    @RestController
    public class UserController {
        @Value("${name}") //使用@Value注入配置文件中的值。${}要加引号
        private String name;
    
        @RequestMapping("/user")
        public String handler(){
            return name; //使用
        }
    
    }

    不能直接${ }、"${ }"来使用配置文件中的值。

    需要借助成员变量,使用@Value注入配置文件中的值,通过成员变量来引用。

    不管成员变量是什么数据类型,${ }都需要加引号,会自动转换为需要的类型,注入。

    对象、Map,通过.来注入单个字段, @Value("${student.name}")

    数组、List,通过下标来注入单个元素,@Value("${city[0]}")

    只能注入基本类型,不能直接直接注入整个对象、Map、数组、List。

    (2)使用@ConfigurationProperties注入对象、Map

    使用@Value依次注入对象、Map的字段时,student.id,student.name,student.age,都有相同的前缀student,也可以这样来注入:

    @RestController
    @ConfigurationProperties(prefix = "student") //设置前缀
    public class UserController {
        private int id;
        private String name;
        private int age;
        private int score;
    
        public void setId(int id) {
            this.id = id;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public void setScore(int score) {
            this.score = score;
        }
    
        @RequestMapping("/user")
        public String handler(){
            return name; //使用
        }
    
    }

    设置前缀、设置对应的成员变量、并提供对应的setter方法,会自动注入该字段的值。

    运行,效果正常,但IDEA提示:

     其实没啥影响,当然也可以在pom.xml中添加:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

    这样写完@ConfigurationProperties后,在yml中配置该前缀(对象)时,会有字段提示,比如打一个student.,会有预选项id、name、age、score。

    小惊喜 

     1、积少成多,下载高佣联盟,领取各大平台隐藏优惠券,每次购物省个十块八块不香吗。

    使用方法:复制商品链接打开高佣联盟自动弹出优惠券,点击跳转到对应平台app内领券购买,或者直接高佣联盟内搜索有优惠券商品点击跳转到淘宝等平台领券购买。

    通过下方二维码注册的用户可添加微信liershuang123(微信号)领取价值千元海量学习视频。

    为表诚意奉献部分资料:

    软件电子书:链接:https://pan.baidu.com/s/1_cUtPtZZbtYTF7C_jwtxwQ 提取码:8ayn
    架构师二期:链接:https://pan.baidu.com/s/1yMhDFVeGpTO8KTuRRL4ZsA 提取码:ui5v
    架构师阶段课程:链接:https://pan.baidu.com/s/16xf1qVhoxQJVT_jL73gc3A 提取码:2k6j

              

     2、本人重金购买付费前后端分离脚手架源码一套,现10元出售,加微信liershuang123获取源码

     

     

  • 相关阅读:
    Spring入门之一-------实现一个简单的IoC
    SpringBoot+SpringSecurity之多模块用户认证授权同步
    SpringBoot+SpringSecurity之如何forword到登录页面
    SpringBoot实现OAuth2认证服务器
    SpringBoot安全认证Security
    SpringBoot的ApplicationRunner和CommandLineRunner
    SpringBoot通过ApplicationArguments获取args
    安利demo
    多路canvas的mapbox gl
    复合canvas叠加显示
  • 原文地址:https://www.cnblogs.com/leskang/p/12654528.html
Copyright © 2011-2022 走看看