Skip to content

META-INF/services convention

The META-INF/services convention is a standard Java service-provider loading facility used for implementing service discovery.^[600-developer__java__black-technology__ServiceLoader.md]

It operates by placing a configuration file within a specific directory structure to declare implementation classes, which are then located and loaded by the java.util.ServiceLoader utility.^[600-developer__java__black-technology__ServiceLoader.md]

Implementation

To utilize this convention, a configuration file must be created inside the META-INF/services/ directory.^[600-developer__java__black-technology__ServiceLoader.md] The name of this file must match the fully qualified name of the service interface.^[600-developer__java__black-technology__ServiceLoader.md] The content of the file should contain the fully qualified names of the concrete classes that implement that service interface.^[600-developer__java__black-technology__ServiceLoader.md]

Usage Example

This mechanism is widely used for modular components, such as JDBC drivers.^[600-developer__java__black-technology__ServiceLoader.md] The java.sql.DriverManager class, for instance, uses ServiceLoader to scan for drivers packaged as Service Providers.^[600-developer__java__black-technology__ServiceLoader.md]

During initialization, the DriverManager invokes ServiceLoader.load(Driver.class), which reads the configuration files from the classpath to identify available driver implementations.^[600-developer__java__black-technology__ServiceLoader.md]

Sources

  • 600-developer__java__black-technology__ServiceLoader.md