@ConfigurationProperties读取配置文件内容
1.class
import lombok.Builder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Builder
@Data
@Component
@ConfigurationProperties(prefix = "chao")
public class ChannelListDTO {
private List<ChannelDTO> list;
}
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ChannelDTO {
private Boolean mg;
private String fulfillmentMethod;
private String segment;
private String channel;
}
2.application-chao.yml
channel-management-list:
list[0]:
mg: true
fulfillmentMethod: Direct
segment: SMB,MM,ED,LE,GA,PS
channel: 21
list[1]:
mg: true
fulfillmentMethod: Indirect
segment: SMB,MM,ED,LE,GA,PS
channel: 11
list[2]:
mg: false
fulfillmentMethod: Direct
segment: MM,SMB
channel: 71
3.使用时直接注入即可
@Autowired
private ChannelListDTO channelListDTO;
public void test(){
channelListDTO.getList();
}
具体例子,参考
org.springframework.boot.autoconfigure.web.ServerProperties源码