Spring 3.1 + Tomcat
I have a bit of a design question here:
There are a group of categories that have been specified in a database. These categories can be considered global in the sense that they could be used throughout the webapp. What I would like to do is read these categories on server startup and populate some type of collection in Java. The only need to be read from the database once at startup, consider it a type of initialization.
Two options I can think of:
1) Should I use a NON lazily initialized bean?
or
2) Modifying the web.xml?
I’m not really sure what is the preferred method, and any instructions on how to perform your recommended would be much appreciated. Thanks!
Options you have provided are most commonly used:
Use singleton non-lazy bean with a method annotated with
@PostConstruct(but be aware that@Transactionalmight not work). You can have several beans with such initialization routine.Extend
org.springframework.web.context.ContextLoaderListenerand use it inweb.xml. I find this solution less elegant and also promoting bad programming style (extending with call tosuperto enhance the base class)