As far as I understand are spring controllers stateless right? If I set a field to a value, this is revoked by the next call.
I believe that is incorrect: Spring controllers can be stateful. You should be very careful though because a Controller is expected to be re-entrant and thread-safe and to support multiple threads simultaneously executing multiple requests.
It is probably safe to say that it best practice for a controller to be designed to be effectively stateless; i.e. no state that changes while the controller is "live".
Is there a possibility to add a init-method or something?
It is not entirely clear what you mean. However:
- The controller's
handleRequest
is called to start a request.
- If you declare any bean (e.g. a controller bean) as
ApplicationContextAware
it will be called back to inform it of the ApplicationContext.
- If you declare any bean as
ServletContextAware
it will be called back to inform it of the ServletContext.
- If you declare any bean as an
InitializingBean
it will be called back when all properties have been set.
And there are doubtless other callbacks and hooks that you could use to trigger some delayed initialization / context setting.
(I'm not sure how these callbacks / hooks map to annotations ... but I'm sure that they do.)