@ConfigurationProperties映射List<Bean>和Map<String,Bean>

@ConfigurationProperties映射List和Map

ws:
  clients:
    test:
      url: ws://127.0.0.1:8888
      name: test1
    dev:
      url: ws://127.0.0.1:9999
      name: dev1

ws2:
  clients:
    - url: ws://127.0.0.1:8888
      name: test2
    - url: ws://127.0.0.1:9999
      name: dev2


映射Map

package com.zhongma.coremodule.ws;

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

import java.util.Map;
import java.util.Set;

/**
 * @author konglcwqy
 * @description
 * @date 2023/5/11 12:31
 */
@Configuration
@ConfigurationProperties(prefix = "ws")
@Data
public class WsConfig {

    private Map<String, WsClient> clients;

    public void listMap() {
        Set<Map.Entry<String, WsClient>> entries = clients.entrySet();
        for (Map.Entry<String, WsClient> entry : entries) {
            System.out.println(entry.getKey() + "=" + entry.getValue().getUrl() + "_"+entry.getValue().getName());
        }
    }
}

映射List

package com.zhongma.coremodule.ws;

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

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

/**
 * @author konglcwqy
 * @description
 * @date 2023/5/11 12:31
 */
@Configuration
@ConfigurationProperties(prefix = "ws2")
@Data
public class WsConfig2 {

    private List<WsClient> clients;

    public void listMap() {
        for (WsClient client : clients) {
            System.out.println(client.getUrl()+"----------------"+client.getName());
        }
    }
}

你可能感兴趣的:(list,java,spring)