自訂 Java 對類別路徑資源載入的 URL 支援
在 Java 中,開發人員可以利用各種 URL 協定存取多功能資源載入 API。然而,雖然「file://」和「http://」等協定有助於從特定位置檢索資源,但可能需要從應用程式的類別路徑存取資源,而無需明確指定 jar 或類別資料夾。本文探討了滿足此要求的解決方案,引入了基於類別路徑的資源載入的自訂 URL 協定和流程處理程序。
實作自訂流程處理程序
解決方案的核心是一個開啟的 URLStreamHandler到指定 URL 的連線。此處理程序必須使用「簡單」套件名稱作為支援的協定向 URL 類別註冊。
package org.my.protocols.classpath; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; public class Handler extends URLStreamHandler { private final ClassLoader classLoader; public Handler() { this.classLoader = getClass().getClassLoader(); } @Override protected URLConnection openConnection(URL u) /*throws IOException*/ { final URL resourceUrl = classLoader.getResource(u.getPath()); return resourceUrl.openConnection(); } }
使用自訂協定
要使用自訂協定載入資源,請使用下列語法:
new URL("classpath:org/my/package/resource.extension").openConnection();
透過指定相對於類路徑的路徑,可以在不指定資源在類路徑中的具體位置的情況下加載資源
增強處理
為了確保正確處理從類路徑加載的資源,可能有必要克服啟動配置和JVM 級處理程序註冊的問題。
手動處理程序規範
作為自動處理程序註冊的替代方案,可以在URL 期間手動指定處理程序創建:
new URL( null, "classpath:some/package/resource.extension", new org.my.protocols.classpath.Handler(ClassLoader.getSystemClassLoader()) );
JVM 級處理程序註冊
對於應用程式範圍的類路徑資源加載,建議向JVM 註冊URLStreamHandlerFactory。這允許自訂處理程序用於所有 URL:
package my.org.url; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; import java.util.HashMap; import java.util.Map; class ConfigurableStreamHandlerFactory implements URLStreamHandlerFactory { private final Map<string urlstreamhandler> protocolHandlers; public ConfigurableStreamHandlerFactory(String protocol, URLStreamHandler urlHandler) { protocolHandlers = new HashMap(); addHandler(protocol, urlHandler); } public void addHandler(String protocol, URLStreamHandler urlHandler) { protocolHandlers.put(protocol, urlHandler); } public URLStreamHandler createURLStreamHandler(String protocol) { return protocolHandlers.get(protocol); } }</string>
要註冊工廠,請呼叫 URL.setURLStreamHandlerFactory() 傳遞配置的工廠。
結論
透過建立自訂 URLStreamHandler 和流處理程序工廠,可以擴展 Java 的資源載入功能以包含位於應用程式類別路徑上的資源。這提供了更大的靈活性並簡化了從各種來源加載資源的過程。
以上是如何自訂 Java 的 URL 支援以直接從類別路徑載入資源?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生產性。 1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允許CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java實現“一次編寫,到處運行”通過編譯成字節碼並在Java虛擬機(JVM)上運行。 1)編寫Java代碼並編譯成字節碼。 2)字節碼在任何安裝了JVM的平台上運行。 3)使用Java原生接口(JNI)處理平台特定功能。儘管存在挑戰,如JVM一致性和平台特定庫的使用,但WORA大大提高了開發效率和部署靈活性。

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允許Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,對象與偏見,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的頂級功能包括:1)面向對象編程,支持多態性,提升代碼的靈活性和可維護性;2)異常處理機制,通過try-catch-finally塊提高代碼的魯棒性;3)垃圾回收,簡化內存管理;4)泛型,增強類型安全性;5)ambda表達式和函數式編程,使代碼更簡潔和表達性強;6)豐富的標準庫,提供優化過的數據結構和算法。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具