Java 元信息服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4544899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:53:00  来源:igfitidea点击:

Java Meta-Inf Services

javameta-inf

提问by dialloc

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

Java 中的 Meta-INF 服务的目的是什么?

回答by kschneid

Take a look at the ServiceLoaderdocs.

看看ServiceLoader文档

回答by Marco Sigismondi

It's intended to store service provider configuration files.

它旨在存储服务提供者配置文件。

A Service provider is an implementation of a Service Provider Interface packaged as JAR.

服务提供者是打包为 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.

以下是 Servlet 3.0 在 webapp 启动时使用的 javax.servlet.ServletContainerInitializer 的提供程序配置文件示例。

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
  • Tomcat 是服务加载器;
  • javax.servlet.ServletContainerInitializer 是服务提供者接口
  • 名为javax.servlet.ServletContainerInitializer的文件是Service Provider的配置文件;
  • org.apache.jasper.servlet.JasperInitializer 和 org.springframework.web.SpringServletContainerInitializer 是服务提供者

When tomcat startup webapp call both

当tomcat启动webapp时同时调用

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

metods on JasperInitializer and SpringServletContainerInitializer classes

JasperInitializer 和 SpringServletContainerInitializer 类的方法