您好,欢迎来到华拓科技网。
搜索
您的当前位置:首页springboot配置文件yml中的map形式

springboot配置文件yml中的map形式

来源:华拓科技网
tes:
  maps: {key1: 12,key2: 34}

或者

tes:
  maps:
    key1: 15
    key2: 2
  1. 创建一个类,然后创建对应类型的字段(注意下这个类的那两个注释了的注解)
package com.etc.lzg;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Map;

@Data
@Component
//@Configuration //这个我这里虽然存在时能成功,不过我注释了也是可以的,这个是看网上有人写就跟着写上的
//@PropertySource(value = {"classpath:/application.yml"}, encoding = "utf-8") //有的人是写了这个注解能成功,但是我这边不能有这个注解,有的话,就连编译都会报错
@ConfigurationProperties(prefix = "tes")
public class MapTest {
    private Map<String, String> maps;
}
  1. 引用
package com.etc.lzg;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LzgApplicationTests {

    @Autowired
    private MapTest mapTest;
    
    @Test
    public void contextLoads() {
        System.out.println(mapTest.toString());
        System.out.println("key1="+mapTest.getMaps().get("key1"));
    }

}

  1. 打印

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo6.cn 版权所有 赣ICP备2024042791号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务