简体   繁体   中英

how to read a file and load an object on application startup spring boot

I am having Yaml file that contains configuration for example navigation items, names for plugins, etc,. currently I am reading this file in component class and have to inject it in every class, is there any way that I can read and load this .yml file on application startup make it available application wide and create html file without injecting in every class and calling it in every method ?

this is how i am reading file.

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
    }

这听起来像是@ConfigurationProperties理想用例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM