---
title: "spring booot 外部文件加载"
author: "marlay"
date: "2017-05-15T09:34:58Z"
tags: []
categories: ["编程语言"]
slug: "spring-booot"
draft: false
lastmod: "2017-05-15T09:34:58Z"
id: 2374308
url: "https://marlay.iteye.com/blog/2374308"
---

1. **场景**
   
   有一个项目需要加载一个自定义的xml文件。正常情况下，我们把资源文件放到src/main/resources目录下。这样就有一个问题，这里面的资源文件会被一起打包到jar里面。  
   
   如果只是spring本身的文件还好说，因为spring boot会加载jar同级目录以及与jar同级的conf目录下的application\*.properties文件。而我们的xml文件，不是spring加载的，是我们自己写的解析。  
   
   这样发布后，只能从classpath加载，也就是jar包本身里面。现在要求实现类spring这种方式来加载。
   
   先通过下图看看原来的加载方式，直接中classpath中加载，代码量极少。
   
   [![spring boot应用加载外部自定义资源文件](https://imgsa.baidu.com/exp/w=500/sign=b6b54ee8c5bf6c81f7372ce88c3fb1d7/d53f8794a4c27d1ef82c914412d5ad6eddc43823.jpg)](http://jingyan.baidu.com/album/af9f5a2d64d97f43150a4573.html?picindex=1)
   
   [![spring boot应用加载外部自定义资源文件](https://imgsa.baidu.com/exp/w=500/sign=bf4277e8c5bf6c81f7372ce88c3fb1d7/d53f8794a4c27d1ef1dba84412d5ad6edcc438b4.jpg)](http://jingyan.baidu.com/album/af9f5a2d64d97f43150a4573.html?picindex=2)
2. **方案**
   
   首先我们可以获取到jar本身路径，然后直接从jar路径找xml文件，找不到的时候在找同级的conf目录，还是没有的话，再使用classpath下的xml文件。 这样就可以把配置文件放到jar的外面了。以后有修改，就不需要在重新打包了。
   
   加载资源文件的代码如下：
   
   File file = new File("client-system.xml") // 这里表示从jar同级目录加载
   
    
   
   if (!file.exists()) { // 如果同级目录没有，则去config下面找
   
       file = new File("config/client-system.xml");
   
   }
   
   Resource resource = new FileSystemResource(file);
   
   if (!resource.exists()) { //config目录下还是找不到，那就直接用classpath下的
   
       resource = resourceLoader.getResource("classpath:client-system.xml");
   
   }
   
   SAXReader reader = new SAXReader();
   
   Document doc = reader.read(resource.getInputStream());
   
   //.....到此资源文件就可以从三个预选路径中加载了
   
   //.....省略后面的对xml操作等代码
   
    
   
    
   
   最后配置文件可以如下图方式存放，client-system.xml就是我们的自定义配置文件，
   
   [![spring boot应用加载外部自定义资源文件](https://imgsa.baidu.com/exp/w=500/sign=864e1824adc27d1ea5263bc42bd4adaf/f636afc379310a55e1687474be4543a9832610e5.jpg)](http://jingyan.baidu.com/album/af9f5a2d64d97f43150a4573.html?picindex=3)
   
   [![spring boot应用加载外部自定义资源文件](https://imgsa.baidu.com/exp/w=500/sign=f6c042a7763e6709be0045ff0bc69fb8/34fae6cd7b899e515ef584944ba7d933c8950d32.jpg)](http://jingyan.baidu.com/album/af9f5a2d64d97f43150a4573.html?picindex=4)
3. **编写测试**
   
   在加载xml文件的地方加入了打印输出，为了方便直观没有用Logger了，直接使用System.out.println了。代码如下：
   
   [![spring boot应用加载外部自定义资源文件](https://imgsa.baidu.com/exp/w=500/sign=96d31081556034a829e2b881fb1149d9/9e3df8dcd100baa1c2e7de3b4e10b912c9fc2e62.jpg)](http://jingyan.baidu.com/album/af9f5a2d64d97f43150a4573.html?picindex=5)
4. **运行jar包**
   
   通过java -jar 来运行打好的jar文件。再看日志输出，与左边的文件进行对比。
   
   第一种方式将xml和资源文件放到jar同级目录；
   
   第二方式将xml放到jar同级的config目录；
   
   第三种方式就是默认方式，从jar内部加载。
   
   每种方式的输出结果请留意控制台中的高亮部分。
   
   [![spring boot应用加载外部自定义资源文件](https://imgsa.baidu.com/exp/w=500/sign=e125982b0a3b5bb5bed720fe06d2d523/00e93901213fb80eb2fdf8573fd12f2eb9389406.jpg)](http://jingyan.baidu.com/album/af9f5a2d64d97f43150a4573.html?picindex=6)
   
   [![spring boot应用加载外部自定义资源文件](https://imgsa.baidu.com/exp/w=500/sign=402900845082b2b7a79f39c401accb0a/95eef01f3a292df501933495b5315c6035a873da.jpg)](http://jingyan.baidu.com/album/af9f5a2d64d97f43150a4573.html?picindex=7)
   
   [![spring boot应用加载外部自定义资源文件](https://imgsa.baidu.com/exp/w=500/sign=1bd8799a6959252da3171d04049a032c/adaf2edda3cc7cd94f31af593001213fb80e9116.jpg)](http://jingyan.baidu.com/album/af9f5a2d64d97f43150a4573.html?picindex=8)
   
   [![spring boot应用加载外部自定义资源文件](https://imgsa.baidu.com/exp/w=500/sign=89f5a4cb0b087bf47dec57e9c2d2575e/6a600c338744ebf88e2cffbfd0f9d72a6159a7d5.jpg)](http://jingyan.baidu.com/album/af9f5a2d64d97f43150a4573.html?picindex=9)
   
   [![spring boot应用加载外部自定义资源文件](https://imgsa.baidu.com/exp/w=500/sign=3be77cab07f41bd5da53e8f461d881a0/0b55b319ebc4b74505502c96c6fc1e178b82156d.jpg)](http://jingyan.baidu.com/album/af9f5a2d64d97f43150a4573.html?picindex=10)
   
   END