Spring Boot access static resources missing scr/main/resources

前端 未结 8 499
别那么骄傲
别那么骄傲 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: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.

提交回复
热议问题