2

I'd like to have a bean that runs through all our FreeMarker templates and instantiates a FreeMarkerView for each one, so that the beans are preloaded. There've been a couple other similar optimisations that I'd like to do that require an HttpServletRequest.

So my question is this: is there any normal way in Spring to run an init() method on a Spring bean during the application startup and provide it with some sort of HttpServletRequest? I'd like to avoid mocking out a full HttpServletRequest, and I know I could always setup a controller and open a socket and send a request to it, but I wanted to know if there was any better or more normal way of doing it.

Surely there must be other people who've wanted to set up an HttpServletRequest without making an HTTP request, either at startup or on a scheduled task, etc.?

  • Have a look at this Stack Overflow Question: [How can I make Tomcat pre-compile JSPs on startup?](http://stackoverflow.com/questions/497830/how-can-i-make-tomcat-pre-compile-jsps-on-startup) It is about JSPs, but I think you can reuse there ideas for your topic. – Ralph Feb 03 '12 at 15:11

1 Answers1

4

When the application is starting there are no HTTP servlet requests yet. In fact, Tomcat during bootup starts listening and accepting connections on 8080 port, but passes them to appropriate servlets only when all applications have successfully started. This means you cannot call yourself during startup since this will lead to deadlock - you are waiting for a response which is blocked by Tomcat that waits for you.

This also means that it is simply impossible (at least when Tomcat is taken as an exmaple) to obtain any HTTP servlet request during startup as there are absolutely no requests yet. Yes, mocking is the correct way, but I'm afraid if these libraries require an HttpServletRequest instance, simple mock might not be enough.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Got it. And it would be too much to hope that there's some sort of post-startup hook that I could use to kick off a method? And even if there were or I built one myself, how would I go about creating an `HttpServletRequest` short of opening a connection to myself? – Jun-Dai Bates-Kobashigawa Feb 03 '12 at 15:52