简体   繁体   English

休息服务的应用程序类生命周期是什么?

[英]What is that Application class lifecycle of a rest service?

Is every rest service starting with extending that application class and defining applicationpath?每个休息服务都是从扩展该应用程序类并定义应用程序路径开始的吗? What is the lifecyce of that application class itself?该应用程序类本身的生命周期是多少? Here is an example:下面是一个例子:

import javax.ws.rs.core.Application;
@javax.ws.rs.ApplicationPath("resources")
public class ApplicationConfig extends Application {}

Is this a servlet?这是一个servlet吗? Is it always alive?它永远活着吗? How shall I understand this class?我该如何理解这门课? Is it a cdi bean?是cdi豆吗? Does the server creates this class on every request?服务器是否在每个请求上创建这个类?

What is Application ?什么是Application

Application is a deployment agnostic abstract class provided by JAX-RS for configuring and registering the components of a JAX-RS application and it's also used to supply additional metadata to the application. Application是 JAX-RS 提供的一个与部署无关的抽象类,用于配置和注册 JAX-RS 应用程序的组件,它还用于为应用程序提供额外的元数据。

Application is one of the types that can be injected using the @Context annotation. Application是可以使用@Context注释注入的类型之一。 For more details, refer to this answer .有关更多详细信息,请参阅此答案

Subclasses of Application Application子类

Application subclasses can implement methods such as getClasses() , getSingletons() and getProperties() for configuring and registering components and properties. Application子类可以实现诸如getClasses()getSingletons()getProperties()来配置和注册组件和属性。

Application subclasses can be annotated with @ApplicationPath , defining the base URI for the JAX-RS resource classes (classes annotated with @Path ). Application子类可以用@ApplicationPath注释,定义 JAX-RS 资源类(用@Path注释的类)的基本 URI。 Application subclasses are instantied once when the web application starts and they are managed by the JAX-RS runtime. Application子类在 Web 应用程序启动时实例化一次,并由 JAX-RS 运行时管理。

The simplest implementation possible is as following:最简单的实现可能如下:

@ApplicationPath("api")
public SampleApplication extends Application {

}

In the example above no resources classes or providers are registered, so the JAX-RS runtime will scan the classpath for JAX-RS components and will register them automatically.在上面的示例中,没有注册资源类或提供者,因此 JAX-RS 运行时将扫描 JAX-RS 组件的类路径并自动注册它们。

However, according to this post from Jakub Podlesak , this approach is discouraged in production environments:但是,根据Jakub Podlesak 的这篇文章,不鼓励在生产环境中使用这种方法:

The above example works great.上面的例子效果很好。 When started, the application just scans the actual class-path, and adds every single JAX-RS component class found there to the actual runtime configuration.启动时,应用程序只扫描实际的类路径,并将在那里找到的每个 JAX-RS 组件类添加到实际的运行时配置中。 Isn't is great?是不是很棒? Frankly, this kind of configuration could work just fine.坦率地说,这种配置可以正常工作。 Until someone changes either the system configuration (system class-path) or the way how you application is being packaged (a new 3rd party component could be added/removed from the application class-path then).直到有人更改系统配置(系统类路径)或应用程序的打包方式(然后可以从应用程序类路径中添加/删除新的第 3 方组件)。 These changes could be out of your control and if one of them happens, you application configuration could break.这些更改可能超出您的控制,如果其中之一发生,您的应用程序配置可能会中断。 For this reason, it is not wise to use this kind of configuration in a production environment.因此,在生产环境中使用这种配置是不明智的。

Jersey, the JAX-RS reference implementation, provides the ResourceConfig class. Jersey 是 JAX-RS 参考实现,提供了ResourceConfig类。 Compared to Application , ResourceConfig provides advanced capabilities to simplify registration of JAX-RS components, such as scanning for root resource and provider classes in a provided classpath or a in a set of package names, etc. For more details, refer to the Jersey documentation .Application相比, ResourceConfig提供了高级功能来简化 JAX-RS 组件的注册,例如扫描提供的类路径或一组包名称中的根资源和提供程序类等。更多详细信息,请参阅Jersey 文档.

Working with multiple Application subclasses使用多个Application子类

Is also worth mentioning that you are not restricted to a single Application subclass per web application.还值得一提的是,您并不受限于每个 Web 应用程序的单个Application子类。 The same WAR can have multiple Application subclasses.同一个 WAR 可以有多个Application子类。 For more details, have a look at this post from Adam Bien :有关更多详细信息,请查看Adam Bien 的这篇文章

To deploy multiple JAX-RS applications with different URIs in one WAR you will have to create one javax.ws.rs.core.Application subclass per such an application (or use web.xml for this purpose).要在一个 WAR 中部署多个具有不同 URI 的 JAX-RS 应用程序,您必须为每个这样的应用程序创建一个javax.ws.rs.core.Application子类(或为此使用web.xml )。 Obviously the in Java EE ubiquitous Convention over Configuration (or Configuration by Exception) cannot work any more: you will have to explicitly configure resources in each subclass by overriding the method getClasses or getSingletons :显然,Java EE 中普遍存在的配置约定(或异常配置)不再起作用:您必须通过覆盖方法getClassesgetSingletons来显式配置每个子类中的资源:

 @Path("first") public class FirstResource { @GET public String first() { return "first"; } }
 @ApplicationPath("one") public class JAXRSConfigurationOne extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new HashSet<>(); resources.add(FirstResource.class); return resources; } }
 @Path("second") public class SecondResource { @GET public String first() { return "second"; } }
 @ApplicationPath("two") public class JAXRSConfigurationTwo extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> resources = new HashSet<>(); resources.add(SecondResource.class); return resources; } }

Both JAX-RS applications become accessible through distinct URIs: http://localhost:8080/multiple-roots/one/first and http://localhost:8080/multiple-roots/two/second两个 JAX-RS 应用程序都可以通过不同的 URI 访问: http://localhost:8080/multiple-roots/one/firsthttp://localhost:8080/multiple-roots/two/second

What if no Application subclass is present?如果不存在Application子类怎么办?

If no Application subclass is present, the JAX-RS implementations are required to add a servlet and set its name to javax.ws.rs.Application and to automatically discover all resource classes and providers which must be packaged with the application.如果不存在Application子类,则需要 JAX-RS 实现添加 servlet 并将其名称设置为javax.ws.rs.Application并自动发现必须与应用程序一起打包的所有资源类和提供者。

For further details, have a look at the chapter 2 of the JAX-RS 2.1 specification .有关更多详细信息,请查看JAX-RS 2.1 规范的第 2 章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM