在Spring Boot中從類路徑加載文件的示例
資源加載器
使用Java,您可以使用當(dāng)前線程的classLoader并嘗試加載文件,但是Spring Framework為您提供了更為優(yōu)雅的解決方案,例如ResourceLoader。
您只需要自動連接ResourceLoader,然后調(diào)用getResource(„somePath“)方法即可。
在Spring Boot(WAR)中從資源目錄/類路徑加載文件的示例
在以下示例中,我們從類路徑中加載名為GeoLite2-Country.mmdb的文件作為資源,然后將其作為File對象檢索。
@Service("geolocationservice")
public class GeoLocationServiceImpl implements GeoLocationService {
private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class);
private static DatabaseReader reader = null;
private ResourceLoader resourceLoader;
@Autowired
public GeoLocationServiceImpl(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
} @PostConstruct
public void init() {
try {
LOGGER.info("GeoLocationServiceImpl: Trying to load GeoLite2-Country database...");
Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb");
File dbAsFile = resource.getFile(); // Initialize the reader
reader = new DatabaseReader
.Builder(dbAsFile)
.fileMode(Reader.FileMode.MEMORY)
.build();
LOGGER.info("GeoLocationServiceImpl: Database was loaded successfully.");
} catch (IOException | NullPointerException e) {
LOGGER.error("Database reader cound not be initialized. ", e);
}
}
@PreDestroy
public void preDestroy() {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
LOGGER.error("Failed to close the reader.");
}
}
}
}
在Spring Boot(JAR)中從資源目錄/類路徑加載文件的示例
如果您想從Spring Boot JAR中的 classpath加載文件,則必須使用該resource.getInputStream()方法將其作為InputStream檢索。如果嘗試使用resource.getFile()該方法,則會收到錯誤消息,因?yàn)镾pring嘗試訪問文件系統(tǒng)路徑,但無法訪問JAR中的路徑。
@Service("geolocationservice")
public class GeoLocationServiceImpl implements GeoLocationService {
private static final Logger LOGGER = LoggerFactory.getLogger(GeoLocationServiceImpl.class);
private static DatabaseReader reader = null;
private ResourceLoader resourceLoader;
@Inject
public GeoLocationServiceImpl(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
} @PostConstruct
public void init() {
try {
LOGGER.info("GeoLocationServiceImpl: Trying to load GeoLite2-Country database...");
Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb");
InputStream dbAsStream = resource.getInputStream(); // <-- this is the difference
// Initialize the reader
reader = new DatabaseReader
.Builder(dbAsStream)
.fileMode(Reader.FileMode.MEMORY)
.build();
LOGGER.info("GeoLocationServiceImpl: Database was loaded successfully.");
} catch (IOException | NullPointerException e) {
LOGGER.error("Database reader cound not be initialized. ", e);
}
}
@PreDestroy
public void preDestroy() {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
LOGGER.error("Failed to close the reader.");
}
}
}
}
以上就是在Spring Boot中從類路徑加載文件的示例的詳細(xì)內(nèi)容,更多關(guān)于spring boot 加載文件的資料請關(guān)注腳本之家其它相關(guān)文章!
- 淺談SpringBoot2.4 配置文件加載機(jī)制大變化
- SpringBoot內(nèi)部外部配置文件加載順序解析
- 詳解springboot啟動時是如何加載配置文件application.yml文件
- 簡單了解springboot加載配置文件順序
- Spring Boot加載配置文件的完整步驟
- Springboot為什么加載不上application.yml的配置文件
- SpringBoot配置文件的加載位置實(shí)例詳解
- spring boot啟動時加載外部配置文件的方法
- spring boot加載第三方j(luò)ar包的配置文件的方法
- 詳解Spring Boot加載properties和yml配置文件
相關(guān)文章
Spring?MVC DispatcherServlet處理請求過程示例詳解
這篇文章主要介紹了Spring?MVC?DispatcherServlet處理請求過程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
通過Java實(shí)現(xiàn)中文分詞與文本關(guān)鍵詞提取
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)中文分詞以及文本關(guān)鍵詞提取功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)2023-06-06
Java之SpringCloudAlibaba Sentinel組件案例講解
這篇文章主要介紹了Java之SpringCloudAlibaba Sentinel組件案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
java中volatile不能保證線程安全(實(shí)例講解)
下面小編就為大家?guī)硪黄猨ava中volatile不能保證線程安全(實(shí)例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Java實(shí)現(xiàn)獲取銀行卡所屬銀行,驗(yàn)證銀行卡號是否正確的方法詳解
這篇文章主要介紹了Java實(shí)現(xiàn)獲取銀行卡所屬銀行,驗(yàn)證銀行卡號是否正確的方法,結(jié)合實(shí)例形式詳細(xì)分析了java判斷銀行卡歸屬地及有效性的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-09-09

