自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在Spring Boot中從類路徑加載文件

開發(fā)
創(chuàng)建Spring Boot Web應(yīng)用程序時,有時有時需要從類路徑中加載文件;war和jar的加載文件格式是不一樣的

[[344641]]

在下面,您將找到在WAR和JAR中加載文件的解決方案。

資源加載器
使用Java,您可以使用當(dāng)前線程的classLoader并嘗試加載文件,但是Spring Framework為您提供了更為優(yōu)雅的解決方案,例如ResourceLoader。

您只需要自動連接ResourceLoader,然后調(diào)用getResource(„somePath“)方法即可。

在Spring Boot(WAR)中從資源目錄/類路徑加載文件的示例
在以下示例中,我們從類路徑中加載名為GeoLite2-Country.mmdb的文件作為資源,然后將其作為File對象檢索。

  1. @Service("geolocationservice"
  2. public class GeoLocationServiceImpl implements GeoLocationService { 
  3.     private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class); 
  4.     private static DatabaseReader reader = null
  5.     private ResourceLoader resourceLoader; 
  6.     @Autowired 
  7.     public GeoLocationServiceImpl(ResourceLoader resourceLoader) { 
  8.         this.resourceLoader = resourceLoader; 
  9.     }    @PostConstruct 
  10.     public void init() { 
  11.         try { 
  12.             LOGGER.info("GeoLocationServiceImpl: Trying to load GeoLite2-Country database..."); 
  13.             Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb"); 
  14.             File dbAsFile = resource.getFile();            // Initialize the reader 
  15.             reader = new DatabaseReader 
  16.                         .Builder(dbAsFile) 
  17.                         .fileMode(Reader.FileMode.MEMORY) 
  18.                         .build(); 
  19.             LOGGER.info("GeoLocationServiceImpl: Database was loaded successfully."); 
  20.         } catch (IOException | NullPointerException e) { 
  21.             LOGGER.error("Database reader cound not be initialized. ", e); 
  22.         } 
  23.     } 
  24.     @PreDestroy 
  25.     public void preDestroy() { 
  26.         if (reader != null) { 
  27.             try { 
  28.                 reader.close(); 
  29.             } catch (IOException e) { 
  30.                 LOGGER.error("Failed to close the reader."); 
  31.             } 
  32.         } 
  33.     } 

在Spring Boot(JAR)中從資源目錄/類路徑加載文件的示例
如果您想從Spring Boot JAR中的 classpath加載文件,則必須使用該resource.getInputStream()方法將其作為InputStream檢索。如果嘗試使用resource.getFile()該方法,則會收到錯誤消息,因為Spring嘗試訪問文件系統(tǒng)路徑,但無法訪問JAR中的路徑。

  1. @Service("geolocationservice"
  2. public class GeoLocationServiceImpl implements GeoLocationService { 
  3.     private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class); 
  4.     private static DatabaseReader reader = null
  5.     private ResourceLoader resourceLoader; 
  6.     @Inject 
  7.     public GeoLocationServiceImpl(ResourceLoader resourceLoader) { 
  8.         this.resourceLoader = resourceLoader; 
  9.     }    @PostConstruct 
  10.     public void init() { 
  11.         try { 
  12.             LOGGER.info("GeoLocationServiceImpl: Trying to load GeoLite2-Country database..."); 
  13.             Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb"); 
  14.             InputStream dbAsStream = resource.getInputStream(); // <-- this is the difference 
  15.             // Initialize the reader 
  16.             reader = new DatabaseReader 
  17.                         .Builder(dbAsStream) 
  18.                         .fileMode(Reader.FileMode.MEMORY) 
  19.                         .build(); 
  20.             LOGGER.info("GeoLocationServiceImpl: Database was loaded successfully."); 
  21.         } catch (IOException | NullPointerException e) { 
  22.             LOGGER.error("Database reader cound not be initialized. ", e); 
  23.         } 
  24.     } 
  25.     @PreDestroy 
  26.     public void preDestroy() { 
  27.         if (reader != null) { 
  28.             try { 
  29.                 reader.close(); 
  30.             } catch (IOException e) { 
  31.                 LOGGER.error("Failed to close the reader."); 
  32.             } 
  33.         } 
  34.     } 

 

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2024-10-08 09:06:01

Spring應(yīng)用程序配置

2024-04-23 14:13:38

開發(fā)配置文件

2023-09-22 10:12:57

2024-09-30 11:51:07

2024-01-23 08:47:13

BeanSpring加載方式

2019-12-09 15:08:30

JavaTomcatWeb

2022-09-02 08:19:37

spring配置加載

2011-07-07 17:30:25

iPhone Xcode

2024-06-24 14:52:50

Android類加載器

2019-07-15 10:00:53

DockerJava容器

2021-09-01 10:07:43

開發(fā)零搭建Groovy

2021-10-18 12:04:22

Spring BootJava開發(fā)

2021-10-18 10:36:31

Spring Boot插件Jar

2019-07-15 16:00:24

Docker架構(gòu)容器

2024-10-10 14:43:54

LambdaSpring編程

2024-09-05 09:35:58

CGLIBSpring動態(tài)代理

2022-11-01 08:55:55

編譯工具MavenSpring

2024-09-11 08:22:44

2025-03-28 08:34:34

2024-10-07 08:40:56

Spring應(yīng)用程序Java
點贊
收藏

51CTO技術(shù)棧公眾號