map form springboot profile of yml

  1. yml in the format
tes:
  maps: {key1: 12,key2: 34}

or

tes:
  maps:
    key1: 15
    key2: 2
  1. Create a class, and then create a corresponding type of field (note that this two classes annotated comments)
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. Quote
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. print
    Here Insert Picture Description

Guess you like

Origin blog.csdn.net/MTone1/article/details/91413539