Reputation: 9371
I need to initialize every new http session with some values. How do I do that?
I tried to create a session-scoped component and initializing session in @PostConstruct, but session-scoped beans are not eagerly created until I request access them.
Upvotes: 5
Views: 3068
Reputation: 597016
If you are absolutely certain that your want eager initialization, you can do the following:
defina a <lookup-method>
for that interceptor:
<lookup-method name="getCurrentSessionBean"
bean="yourSessionBeanToInitialize"/>
define the interceptor abstract
, with an abstract
method getCurrentSessionBean()
initialized
on the bean@PostConstruct
and spare the initizlied
flagAnother option is to:
HttpSessionListener
in web.xml (or with annotations if using servlet 3.0)WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
to obtain the contextgetBean(..)
to get an instance of the session-scoped bean@PostConstruct
at that pointThe first option is "more spring", the second is easier and faster to implement.
Upvotes: 3