简体   繁体   English

java jersey静态字段初始化

[英]java jersey static fields initialization

I am creating a REST service with Jersey. 我正在使用Jersey创建REST服务。 Some resources (like some global Maps, files) should be loaded before the first request arrives (because loading this resources need 20 secs). 在第一个请求到达之前,应先加载一些资源(例如某些全局Maps,文件)(因为加载此资源需要20秒)。 So I create some static fields (attributes) to hold these resources, because I believe they should be initialized when I start this REST service. 因此,我创建了一些静态字段(属性)来保存这些资源,因为我认为在启动此REST服务时应将其初始化。

However, I found one strange problem that static resources will be loaded only when I send the first request. 但是,我发现一个奇怪的问题,仅当我发送第一个请求时才会加载静态资源。 So for the first request, it always takes more than 20s to get response, while later requests are much faster (since resources have been loaded). 因此,对于第一个请求,获得响应总是要花费20多个秒,而随后的请求则要快得多(因为已经加载了资源)。

Could anyone explain me why Java doesn't initialize these static fields before any request ? 谁能解释我为什么Java在任何请求之前不初始化这些静态字段?

In Java, classes are initialized the first time they are loaded. 在Java中,类是在首次加载时初始化的。 It is your first request that causes the classes to load. 导致类加载的是您的第一个请求。

Maybe this will help. 也许会有所帮助。

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

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