Reputation: 521
What is the purpose of META-INF/services in Java ?
Upvotes: 52
Views: 31004
Reputation: 421
It's intended to store service provider configuration files.
A Service provider is an implementation of a Service Provider Interface packaged as JAR.
A Service loader discover and load all implementations declared in the service provider configuration file.
A configuration file is a file named as the fully qualified name of the interface and its content is a list of fully qualified names of implementations.
Following is an example of provider configuration file for javax.servlet.ServletContainerInitializer that is used by Servlet 3.0 at webapp startup.
org.apache.jasper.servlet.JasperInitializer
org.springframework.web.SpringServletContainerInitializer
In this example
When tomcat startup webapp call both
onStartup(java.util.Set<java.lang.Class<?>> types, ServletContext context)
methods on JasperInitializer and SpringServletContainerInitializer classes
Upvotes: 30
Reputation: 567
Before Java 9, ServiceLoader find the implementations for a Service from the file in META-INF/services which has fully qualified name same as the Service interface. It contain list of fully qualified names of the implementations.
From Java 9 It have modules and modules have module descriptors. Those 'module' can define the services and their implementation that a ServiceLoader could load.
Upvotes: 0