Java Language ServiceLoader

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

ServiceLoader can be used to get instances of classes extending a given type(=service) that are specified in a file packed in a .jar file. The service that is extended/implemented is often a interface, but this is not required.

The extending/implementing classes need to provide a zero argument constructor for the ServiceLoader to instantiate them.

To be discovered by the ServiceLoader a text file with the name of the fully qualified type name of the implemented service needs to be stored inside the META-INF/services directory in the jar file. This file contains one fully qualified name of a class implementing the service per line.



Got any Java Language Question?