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

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.