SpringBoot properties和yml的区别

一。先附一个yml文件的解析步骤

1.Maven依赖

  <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.10</version>
        </dependency>

2.yml文件

name: hha
age: 60
friend:
       - good
       - easy
       - bug
params:
      addr: ZZ
      code: EE
name: 洗洗

3.实体类

package com.my.last;

import java.util.List;
import java.util.Map;


public class Student {
    
    private String name;
    private int age;
    
    private List<String> friend ;
    
    private Map<String, Object> params;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }

    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * @return the friend
     */
    public List<String> getFriend() {
        return friend;
    }

    /**
     * @param friend the friend to set
     */
    public void setFriend(List<String> friend) {
        this.friend = friend;
    }

    /**
     * @return the params
     */
    public Map<String, Object> getParams() {
        return params;
    }

    /**
     * @param params the params to set
     */
    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", friend=" + friend + ", params=" + params + "]";
    }  

}

4.测试类

package com.my.last;

import org.yaml.snakeyaml.Yaml;

public class Test {
    public static void main(String[] args) {
        Yaml yaml = new Yaml();
        
        Student student = yaml.loadAs(Test.class.getResourceAsStream("/test.yml"), Student.class);
        System.out.println(student);
    }
}

 或者依赖 

<dependency>
            <groupId>org.jyaml</groupId>
            <artifactId>jyaml</artifactId>
            <version>1.3</version>
        </dependency>

解析方法:

        Student student2 = null;
        try {
             student2 = org.ho.yaml.Yaml.loadType(Test.class.getResourceAsStream("/test.yml"), Student.class);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(student2);

二.二者区别

在于其拥有天然的树状结构,所以着手尝试将properties文件更改为yml文件,发现了几个要注意的地方:
1、在properties文件中是以”.”进行分割的, 在yml中是用”:”进行分割;
2、yml的数据格式和json的格式很像,都是K-V格式,并且通过”:”进行赋值;
3、在yml中缩进一定不能使用TAB,否则会报很奇怪的错误;(缩进特么只能用空格!!!!)
4、yml每个k的冒号后面一定都要加一个空格;
5、使用spring cloud的maven进行构造的项目,在把properties换成yml后,一定要进行mvn clean insatll 

6、yml是跨语言的:可以在包括JAVA,go,python等大量的语言中使用,比如做云计算使用go和java的时候,可以通过配置中心使用同一份配置! 

7、支持列表:区别于properties只支持键值对数据,yml配置文件支持列表,如下所示:

 

 

当然,从properties转yml文件会遇到很多坑,在此记录下:

1,层级关系缩进不能用tab键:每次都数2.4.6这样打空格。。。

2,每个key的后面需要加:,每个:后面还需要加一个空格!

3,列表的短横线后面需要有个空格。

两个关键点:

第一个是yml是支持中文内容的,properties想使用中文只能用unicode编码

第二个是顺序问题,properties是不保证加载顺序的,yml有先后顺序,实际用例比如springcloud的zuul网关路由配置,如果一个uri同时满足两个匹配规则,properties你是不知道它到底使用了哪个规则的,而yml则一定是使用了靠的那个路由规则

Java 的 Properties 加载属性文件后是无法保证输出的顺序与文件中一致的,因为 Properties 是继承自 Hashtable , key/value 都是直接存在 Hashtable 中的,而 Hashtable 是不保证进出顺序的。

总有时候会有关心顺序一致的需求,恰如有 org.apache.commons.collections.OrderdMap(其实用 LinkedHashMap 就是保证顺序) 一样,我们也想要有个 OrderdProperties。

详见: https://blog.csdn.net/qq1169091731/article/details/53012071

三。Spring Boot中application.properties和application.yml加载顺序

使用@PropertySource注解加载自定义配置文件,该注解无法加载yml配置文件。使用@Value注解获得文件中的参数值

application.properties和application.yml文件可以放在一下四个位置:

  • 外置,在相对于应用程序运行目录的/congfig子目录里。
  • 外置,在应用程序运行的目录里
  • 内置,在config包内
  • 内置,在Classpath根目录

同样,这个列表按照优先级排序,也就是说,src/main/resources/config下application.properties覆盖src/main/resources下application.properties中相同的属性,如图:

 

 

此外,如果你在相同优先级位置同时有application.properties和application.yml,那么application.properties里面的属性就会覆盖里application.yml的属性

详见:https://www.cnblogs.com/lukelook/p/10583003.html

转载于:https://www.cnblogs.com/lukelook/p/11245832.html

  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值