将Jersey与Spring Boot结合使用 [英] Using Jersey with Spring Boot

查看:81
本文介绍了将Jersey与Spring Boot结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot参考文档中标题为 7.3 JAX-RS和Jersey 提到"所有注册的端点应为具有HTTP资源注释(@GET和其他)的@Components .由于端点是Spring @Component,因此其生命周期由Spring管理,您可以使用@Autowired批注注入依赖项,并使用@Value批注注入外部配置.".

The Spring Boot Reference Documentation in section titled 7.3 JAX-RS and Jersey mentions "All the registered endpoints should be @Components with HTTP resource annotations (@GET and others). Since the Endpoint is a Spring @Component, its lifecycle is managed by Spring and you can use the @Autowired annotation to inject dependencies and use the @Value annotation to inject external configuration".

但是我不在乎是否将依赖项注入或将外部配置注入到我的Jersey资源中,因此我没有将我的Jersey资源注释为@Components.我的应用程序运行正常.

But I don't care to have dependencies injected or external configuration injected into my Jersey resources and hence I did not annotate my Jersey resources as @Components. My app works just fine.

通过阅读Spring Boot参考文档,似乎需要将Jersey资源注册为@Components.但这似乎并非如此.对我来说,这似乎是Spring Boot参考文档中的一个小错误.可能是文档可以从"注册端点为@Components "进行更新; "注册端点可以是@Components ".这有道理吗?

From reading the Spring Boot Reference Documentation, it seemed that registering Jersey resources as @Components was a requirement. But that does not seem to be the case. To me this seems like a small bug in the Spring Boot Reference Documentation. May be the documentation can be updated from "registered endpoints should be @Components" to "registered endpoints can be @Components". Does this make sense?

推荐答案

我确实尝试了一下,发现使用Spring @Component注释对Jersey资源进行注释是可选的.如果您使用该注释,则资源的生命周期将由Spring管理,如果不使用该注释,则生命周期将由Jersey管理.

I did try this out and found that annotating a Jersey resource with the the Spring @Component annotation is optional. Should you use that annotation then the resource's life-cycle will be managed by Spring and if you do NOT use that annotation then the life-cycle will be managed by Jersey.

要注意的一件事是,两者之间在默认情况下如何设置生命周期是有很大区别的.

One important thing to note is that there is major difference in how that life-cycle is programmed by default between the two.

第3.4节所述 《泽西岛用户指南》 默认情况下,根资源类的生命周期是每个请求的生命周期,即,每次请求URI路径与根资源匹配时,都会创建根资源类的新实例.这形成了一个非常自然的编程模型,其中可以使用构造函数和字段,而无需考虑对同一资源的多个并发请求.通常,这不太可能成为性能问题的原因.多年来,JVM的类构造和垃圾回收已大大改善,将创建并丢弃许多对象来服务和处理HTTP请求并返回HTTP响应."

As stated in section 3.4 of the Jersey User Guide "By default the life-cycle of root resource classes is per-request which, namely that a new instance of a root resource class is created every time the request URI path matches the root resource. This makes for a very natural programming model where constructors and fields can be utilized without concern for multiple concurrent requests to the same resource. In general this is unlikely to be a cause of performance issues. Class construction and garbage collection of JVMs has vastly improved over the years and many objects will be created and discarded to serve and process the HTTP request and return the HTTP response."

但是,如 Spring框架文档 第1.5节;默认情况下,bean是单例. " Spring IoC容器正好创建该bean定义所定义的对象的一个​​实例.该单个实例存储在此类单例bean的高速缓存中,并且对该命名bean的所有后续请求和引用都返回该高速缓存的对象."

But as stated in the Spring Framework Documentation section 1.5 ; beans by default are singleton. "Spring IoC container creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object."

因此有区别.默认情况下,普通的Jersey根资源类是按请求实例化的,而对于Spring,当资源使用@Component进行注释时,它将是单例的.即在JVM的生存期内只有一个实例.如果您希望Spring托管的资源具有与常规Jersey资源相同的每个请求生命周期,则除了@Component注释之外,还应该添加Spring @Scope(scopeName = WebApplicationContext.SCOPE_REQUEST)注释.通过添加它,您的资源生命周期现在将按请求进行.

Hence there is a difference. Normal Jersey root resource classes by default are instantiated per-request, while with Spring when a resource is annotated with @Component it will be singleton. ie only one instance for the lifetime of the JVM. If you want Spring managed resources to have the same per-request life-cycle that comes with normal Jersey resources then you should add the Spring @Scope(scopeName = WebApplicationContext.SCOPE_REQUEST) annotation in addition to the @Component annotation. By adding it your resource life-cycle will now be per-request.

这篇关于将Jersey与Spring Boot结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆