搜尋
首頁Javajava教程在Java 9中,何時使用ServiceLoader類別在模組中?

在Java 9中,何时使用ServiceLoader类在模块中呢?

Java 有一個來自java.util 套件的ServiceLoader 類,可以幫助定位 #服務透過在類別路徑中搜索,在運行時提供提供者。對於模組中定義的服務提供者,我們可以查看範例應用程式來聲明帶有服務的模組及其工作原理。

例如,我們有一個「test.app我們需要使用Logger 模組,可以藉助我们需要使用 Logger 模块,可以借助 LoggerFinder 服務從System.getLogger() 工廠方法檢索該Logger。

<strong>module com.tutorialspoint.test.app {
   requires java.logging;
   exports com.tutorialspoint.platformlogging.app;
   uses java.lang.System.LoggerFinder;
}</strong>

下面是test.app.MainApp 類別:

package com.tutorialspoint.platformlogging.app;

public class MainApp {
   private static <strong>Logger </strong>LOGGER = <strong>System.getLogger()</strong>;
   public static void main(String args[]) {
      LOGGER.log();
   }
}

#這是「

測試內的LoggerFinder實作」 .logging」模組:

package com.tutorialspoint.platformlogging.logger;

public class MyLoggerFinder extends LoggerFinder {
   <strong>@Override</strong>
   public Logger getLogger(String name, Module module) {
      // return a Logger depending on name/module
   }
}

在「

test.logging」模組聲明中,我們可以透過「提供 – 」提供 LoggerFinder  服務的實作>」條款。

<strong>module com.tutorialspoint.test.logging {
   provides java.lang.System.LoggerFinder
   with com.tutorialspoint.platformlogging.logger.MyLoggerFinder;
}</strong>

以上是在Java 9中,何時使用ServiceLoader類別在模組中?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3 Mac版

SublimeText3 Mac版

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

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。