天天看點

【Autofac打标簽模式】PropertySource和Value

【 Autofac打标簽模式】開源DI架構擴充位址:

https://github.com/yuzd/Autofac.Annotation/wiki

PropertySource 指定資料源

屬性 說明
Path 檔案路徑,看下面的說明
OrderIndex 檔案源排序,越大越先作為查找對象
Embedded 是否為内嵌資源
[PropertySource("appsettings.json")] //這行也可以直接去掉 因為和預設的一樣
    [Component]
    public class Student9
    {
        [Value("${a9}")]
        public string Name { get; set; }
    }
    
           
【Autofac打标簽模式】PropertySource和Value
【Autofac打标簽模式】PropertySource和Value
  • PropertySource的path可以指定為json 和 xml 檔案
  • PropertySource 如果不指定path 預設path為根目錄的appsettions.json
  • 如果Path的值為 / 開頭 則代表為相對于工程目錄
  • 如果設定 Embedded = true 則代表path的值為 内嵌資源的檔案名稱
  • Path的值也可以為 檔案的完整路徑

Value裝配資料源裡面的值

  • ${xxx} 代表從配置源裡面 擷取屬性名稱為xxx的值
  • #{xxx} 代表啟動SPEL表達式,xxx裡面可以嵌套${yyy}
【Autofac打标簽模式】PropertySource和Value
  • SPEL表達式具體用法可以檢視:https://github.com/yuzd/Spring.EL

如果您覺得閱讀本文對您有幫助,請點一下“推薦”按鈕,您的“推薦”将是我最大的寫作動力!歡迎各位轉載,轉載文章之後須在文章頁面明顯位置給出作者和原文連接配接,謝謝。

繼續閱讀