使用springboot配置文件yml中的map形式

网友投稿 3221 2022-12-17

使用springboot配置文件yml中的map形式

使用springboot配置文件yml中的map形式

目录springboot配置文件yml的map形式1、yml中的格式2、创建一个类3、引用4、打印SpringBoot yaml文件map集合使用yaml文件配置配置文件对应的bean

springboot配置文件yml的map形式

1、yml中的格式

tes:

maps: {key1: 12,key2: 34}

或者

tes:

maps:

key1: 15

key2: 2

2、创建一个类

然后创建对应类型的字段(注意下这个类的那两个注释了的注解)

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 maps;

}

3、引用

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"));

}

}

4、打印

SpringBoot yaml文件map集合使用

yaml文件配置

patform.config:

maps:

person_one:

userName: A

platform: A platform

person_two:

userName: B

platform: B platform

配置文件对应的bean

如果yaml文件不是在application.yaml,则注解需要配置locations属性

@ConfigurationProperties(value="platform.config",locations="classpath:config/applicaion-platform.yaml")

public class ParamConfiguration{

private Map maps =new LinkedHashMap();

/**

set ,get 方法 。。。。

*/

public static class ParamInfo{

private StrivVhkyeng username;

private String platform;

/**

set ,get 方法 。。。。

*/

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:m1 Mac设置多jdk版本并动态切换的实现
下一篇:SpringBoot启动yaml报错的解决
相关文章

 发表评论

暂时没有评论,来抢沙发吧~