2016-04-03 25 views
-1

我有Yaml文件,其中包含配置,例如导航项目,插件名称等。目前我正在阅读组件类中的这个文件,并且必须将它注入到每个类中,是否有任何方法可以在应用程序启动时读取和加载此.yml文件,使其可以在应用程序范围内使用并创建html文件,而无需在每个类中注入在每种方法中调用它?如何读取文件并在应用程序启动时加载对象弹簧启动

这是我如何阅读文件。

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

还不是很清楚你有什么问题。该文本提到了一个yml文件,该代码将几个yml文件解析为Manifest列表。 “而且必须在每个班级注入”:需要注射什么?列表? – Heri

+0

考虑在spring环境中使用spring mechanisme加载yml文件。然后只将Environment注入到可以读取他们想要的每个属性的类中。 – Heri

+0

@Heri - 我有多个yaml文件,这是什么返回列表,我想要在我的应用程序中有这个列表。 –

回答

相关问题