2016-04-03 33 views
-1

ナビゲーション項目やプラグイン名などの設定を含むYamlファイルがあります。現在、私はコンポーネントクラスでこのファイルを読んでいて、すべてのクラスにそれを注入する必要があります。アプリケーション起動時にこの.ymlファイルを読み込んで読み込む方法はありますか?アプリケーションごとに利用可能にし、すべてのクラスにすべての方法でそれを呼び出す?ファイルを読み込み、アプリケーションの起動時にオブジェクトを読み込む方法

これは私がファイルを読み取っている方法です。

List<Manifest> readManifestYamlFiles(String path) { 
     // adding the files content to the list 
     List<Manifest> manifestFiles = [] 
     // variable to hold the fileContents 
     String fileContents = "" 
     // recursively looping over the plugins directory to read the manifest.yml file 
     println path 
     new File(path).eachDirRecurse() { dir -> 
      // looking for only .yml file 
      dir.eachFileMatch(~/.*.yml/) { file -> 
       // set the fileContent to the variable 
       fileContents = new File(file.getPath()).text 
       // map manifest.yml file content 
       Manifest manifest = yamlUtility.mapYamlToObject(fileContents, Manifest.class, new Manifest()) 
       // add content of each file to the list 
       manifestFiles.add(manifest); 
      } 
     } 
     return manifestFiles 
    } 
+0

のための理想的なユースケースのように聞こえます。テキストは1つのymlファイルを表し、コードはいくつかのymlファイルをManifestのリストに解析します。 "すべてのクラスに注入する必要があります":何を注入する必要がありますか?リスト? – Heri

+0

spring環境にspring mechanismeを使用してymlファイルをロードすることを検討してください。それから環境だけをクラスに注入します。クラスは必要なそれぞれのプロパティを読み込みます。 – Heri

+0

@Heri - 私は複数のyamlファイルを持っています。これはリストを返すもので、私はアプリケーション全体を通してこのリストを持ちたいと思っています。 –

答えて

関連する問題