
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
When to use the ServiceLoader class in a module in Java 9?
Java has a ServiceLoader class from java.util package that can help to locate service providers at the runtime by searching in the classpath. For service providers defined in modules, we can look at the sample application to declare modules with service and how it works.
For instance, we have a "test.app" module that we need to use Logger that can be retrieved from System.getLogger() factory method with the help of LoggerFinder service.
module com.tutorialspoint.test.app { requires java.logging; exports com.tutorialspoint.platformlogging.app; uses java.lang.System.LoggerFinder; }
Below is the test.app.MainApp class:
package com.tutorialspoint.platformlogging.app; public class MainApp { private static Logger LOGGER = System.getLogger(); public static void main(String args[]) { LOGGER.log(); } }
This is LoggerFinder implementation inside the "test.logging" module:
package com.tutorialspoint.platformlogging.logger; public class MyLoggerFinder extends LoggerFinder { @Override public Logger getLogger(String name, Module module) { // return a Logger depending on name/module } }
In "test.logging" module declaration, we can provide an implementation of LoggerFinder service with a "provides – with" clause.
module com.tutorialspoint.test.logging { provides java.lang.System.LoggerFinder with com.tutorialspoint.platformlogging.logger.MyLoggerFinder; }
- Related Articles
- Importance of Module Descriptor in a Module in Java 9?
- How to create a module in Java 9?
- What is the use of the "requires" clause in a module-info file in Java 9?
- What is the use of the "export" clause in a module-info file in Java 9?
- When to use an abstract class and when to use an interface in Java?
- When to use the readAllBytes() method of InputStream in Java 9?
- When to use the readNBytes() method of InputStream in Java 9?
- When to use the delayedExecutor() method of CompletableFuture in Java 9?
- When should I use the keyword ‘this’ in a Java class?
- What is the use of the Cleaner class in Java 9?
- What are the characteristics of a module in Java 9?
- What are the benefits of a module in Java 9?
- When to use the ofNullable() method of Stream in Java 9?\n
- What is Module System in Java 9?
- When can we use StackWalker.getCallerClass() method in Java 9?

Advertisements