Java META-INF/services
Asked Answered
P

3

50

What is the purpose of META-INF/services in Java ?

Provincial answered 28/12, 2010 at 9:9 Comment(1)
Related question: https://mcmap.net/q/65359/-what-39-s-the-purpose-of-meta-inf/2987755Hite
I
28

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

  • Tomcat is the Service Loader;
  • javax.servlet.ServletContainerInitializer is the Service Provider Interface
  • file named javax.servlet.ServletContainerInitializer is the Service Provider configuration file;
  • org.apache.jasper.servlet.JasperInitializer and org.springframework.web.SpringServletContainerInitializer are Service providers

When tomcat startup webapp call both

onStartup(java.util.Set<java.lang.Class<?>> types, ServletContext context)

methods on JasperInitializer and SpringServletContainerInitializer classes

Indeterminate answered 11/10, 2018 at 9:30 Comment(0)
N
17

Take a look at the ServiceLoader docs.

Nave answered 28/12, 2010 at 17:6 Comment(1)
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.Recessive
Q
-1

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.

Quadriplegia answered 8/8, 2021 at 10:48 Comment(1)
I think this is wrong. META-INF does not contain modules? Can you point to an example where they do?Neville

© 2022 - 2024 — McMap. All rights reserved.