Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在应用程序启动spring boot时读取文件和加载对象_Java_Spring_Spring Boot - Fatal编程技术网

Java 如何在应用程序启动spring boot时读取文件和加载对象

Java 如何在应用程序启动spring boot时读取文件和加载对象,java,spring,spring-boot,Java,Spring,Spring Boot,我有一个Yaml文件,其中包含配置,例如导航项、插件名称等,。目前我正在组件类中读取这个文件,并且必须在每个类中注入它,有没有任何方法可以在应用程序启动时读取并加载这个.yml文件,使其在应用程序范围内可用,并且创建html文件,而无需在每个类中注入并在每个方法中调用它 这就是我阅读文件的方式 List<Manifest> readManifestYamlFiles(String path) { // adding the files content to the l

我有一个
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
    }
列出ReadManifestyamlFile(字符串路径){
//将文件内容添加到列表中
列表文件=[]
//变量来保存文件内容
字符串fileContents=“”
//递归地在plugins目录上循环以读取manifest.yml文件
println路径
新文件(路径).eachDirress(){dir->
//仅查找.yml文件
dir.eachFileMatch(~/.*.yml/){file->
//将fileContent设置为变量
fileContents=新文件(File.getPath()).text
//map manifest.yml文件内容
Manifest Manifest=YAMULITY.MAPYAMLTObject(fileContents,Manifest.class,new Manifest())
//将每个文件的内容添加到列表中
manifestFiles.add(manifest);
}
}
返回文件
}

这听起来像是一个理想的使用案例

很不清楚您的问题是什么。文本谈到一个yml文件,代码将几个yml文件解析为清单列表。“并且必须在每个类中注入”:需要注入什么?列表?考虑使用Spring MexSime加载到YLL环境中的YML文件。然后只将环境注入类中,这些类可以读取它们想要的每个属性。@Heri-我有多个yaml文件,这就是返回列表的内容,我希望在整个应用程序中都有这个列表。