2017-09-21 41 views
0
#Reports 
reports: 
    PnLReport: 
     reportId: 10 
     path: \\\\pathto\\PnLreport\\ 

    BalanceSheetReport: 
      reportId: 11 
      path: \\\\pathto\\balancesheet\\  

schedule-10: 
    description: Deliver pnl reports 
    report: 10 
    format: PDF, XLS 

我在我的申請Spring Bootapplication.yml文件中定義的上述性質。映射到CONFIGS枚舉類型

  1. 我怎麼能映射repordIdpath性質的enum例如,對於每一個報告類型。例如:

    public enum ReportType{ 
    PNL(...) 
    BALANCE(...); 
    
    private final String reportId; 
    private final String path; 
    
    private ReportType(String reportId, String path) { 
        this.identifier = identifier; 
    } 
    
  2. 接下來,我想下schedule-10屬性將reportIdreport: 10之間的映射來推導一個FileService類例如路徑,這樣我可以看,如果路徑中存在的文件。我怎樣才能做這個映射?

  3. 這是我能想到的唯一方法,我的要求是否有更好的方法呢?

回答

0

您可以使用@Value註釋來讀取配置文件屬性,如下面:

@Value({"reports.PnLReport.reportId"}) 
private final String reportId; 
+0

該問題與如何將配置設置解析爲類而不是原語有關。 – Almund

0

我不知道我會建議使用此枚舉,因爲你正在尋找的是更就像一個可配置的屬性實例。也許考慮使用一個簡單的類來讀取它的兩個實例?

public class ReportType { 

    private Integer reportId; 

    private String path; 

    public String getPath() { 
     return path; 
    } 

    public void setPath(String path) { 
     this.path = path; 
    } 

    public void getReportId() { 
     return reportId; 
    } 

    public Integer setReportId(Integer reportId) { 
     this.reportId = reportId; 
    } 
} 

@Component 
@ConfigurationProperties(prefix = "reports") 
public class ReportTypes { 
    public ReportType PlnReport; 
    public ReportType BalanceSheetReport; 
}