I need to load a bunch of images into my Spring webapp. For performance reasons this should only be done after starting the webapp. The images should be saved in a singleton to access from multiple users, sessions etc
I already build a Java Bean with an initalizer that loads the images, in my controller I inject this bean and get the images. Works, but injecting isn’t the right thing for this.
How can I build a singleton bean to hold the images and only load the images one time per webapp?
Have you considered using EhCache built-in web caching? Just add it to your
web.xml:And configure
SimplePageCachingFiltercache in yourehcache.xml:EhCache will now intercept all client-side requests for static resources, read them once, put them in cache and return to the user. To make it even better, it will even add HTTP
Expiryheaderes so the client won’t call you again for the same resource.Isn’t this approach better compared to manually-written loading and caching in a singleton?