1. 程式人生 > >springboot專案動態獲取配置檔案的配置資訊

springboot專案動態獲取配置檔案的配置資訊

application.yml

web:
  my_name: mqs
  tags: aaa,bbb,ccc,ddd
  like: 學習

配置檔案對應的bean

package com.test.demo.pojo;

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

/**
 * @Author: Mqs
 * @Date: 2018/11/6 14:17
 * @Description:
 */
@Configuration
@ConfigurationProperties(prefix = "web")
@Data
public class WebProperties {

    private String myName;
    private String[] tags;
    private String like;
}

測試

  @Autowired
    private WebProperties webProperties;

    @RequestMapping("/web1")
    public String testWeb1(){
        String str = Arrays.toString(webProperties.getTags()) + "----->>>" + webProperties.getLike() + "----->>>" + webProperties.getMyName();
        return str;
    }