Spring Boot access static resources missing scr/main/resources

前端 未结 8 463
别那么骄傲
别那么骄傲 2020-12-04 16:39

I am working on a Spring Boot application. I need to parse an XML file (countries.xml) on start. The problem is that I do not understand where to put it so that I could acce

相关标签:
8条回答
  • 2020-12-04 17:00

    I use spring boot, so i can simple use:

    File file = ResourceUtils.getFile("classpath:myfile.xml");
    
    0 讨论(0)
  • 2020-12-04 17:06

    To get the files in the classpath :

    Resource resource = new ClassPathResource("countries.xml");
    File file = resource.getFile();
    

    To read the file onStartup use @PostConstruct:

    @Configuration
    public class ReadFileOnStartUp {
    
        @PostConstruct
        public void afterPropertiesSet() throws Exception {
    
            //Gets the XML file under src/main/resources folder
            Resource resource = new ClassPathResource("countries.xml");
            File file = resource.getFile();
            //Logic to read File.
        }
    }
    

    Here is a Small example for reading an XML File on Spring Boot App startup.

    0 讨论(0)
提交回复
热议问题