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
I use spring boot, so i can simple use:
File file = ResourceUtils.getFile("classpath:myfile.xml");
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.