简体   繁体   中英

java jersey static fields initialization

I am creating a REST service with Jersey. Some resources (like some global Maps, files) should be loaded before the first request arrives (because loading this resources need 20 secs). So I create some static fields (attributes) to hold these resources, because I believe they should be initialized when I start this REST service.

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).

Could anyone explain me why Java doesn't initialize these static fields before any request ?

In Java, classes are initialized the first time they are loaded. It is your first request that causes the classes to load.

Maybe this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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