Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "Spring 3 MVC way" to listen to the end session event?

I would like to delete some temporal files when user session finishes. The information associated with the files is stored in an object annotated with @SessionAttributes.

The only way I've found to deal with this is creating an HttpSessionListener.

Is there a higher level, simplified, Springy way to listen to the session end event where I could easily get my annotated object?

like image 937
Juan Calero Avatar asked Mar 06 '12 17:03

Juan Calero


2 Answers

You most likely will need to create a HttpSessionListener.

Another stackoverflow answer:

Detect session timeout in Spring 3/Spring Security 2.0.5

Also and example on how to load spring beans into it:

http://www.mkyong.com/spring/spring-how-to-do-dependency-injection-in-your-session-listener/

like image 191
chrislovecnm Avatar answered Oct 29 '22 00:10

chrislovecnm


Two options to use HttpSessionListener with spring beans:

The first is to use WebApplicationContextUtils.getRequiredApplicationContext(servletContext) to obtain the servlet context. From there you have two sub-options:

  • use getBean(..)
  • If you want to use @Autowired / @Inject use getAutowireCapablyBeanFactory().autowireBean(this). You will have to do this only once (check if the fields are null), because the listener is singleton.

The second option is to use AspectJ and @Configurable on the listener.

like image 38
Bozho Avatar answered Oct 28 '22 22:10

Bozho