Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Meta-Inf Services

Tags:

java

meta-inf

What is the purpose of Meta-INF services in Java ?

like image 927
dialloc Avatar asked Dec 28 '10 09:12

dialloc


People also ask

What is META-INF services in Java?

The META-INF directory, if it exists, is used to store package and extension configuration data, including security, versioning, extension and services.

How does Java ServiceLoader work?

It searches for service providers on your application's class path or in your runtime environment's extensions directory. It loads them and enables your application to use the provider's APIs. If you add new providers to the class path or runtime extension directory, the ServiceLoader class finds them.

What is an SPI in Java?

Service provider interface (SPI) is an API intended to be implemented or extended by a third party. It can be used to enable framework extension and replaceable components.


1 Answers

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

like image 186
Marco Sigismondi Avatar answered Oct 05 '22 14:10

Marco Sigismondi