只需使用Spring类型的ClassPathResource即可。
File file = new ClassPathResource("countries.xml").getFile();只要该文件在类路径中的某个位置,Spring就会找到它。这可以src/main/resources在开发和测试期间。在生产中,它可以是当前运行目录。
编辑: 如果文件位于胖JAR中,则此方法不起作用。在这种情况下,你需要使用:
InputStream is = new ClassPathResource("countries.xml").getInputStream();


