Class SpringVaadinSessionListener
- java.lang.Object
-
- org.dellroad.stuff.vaadin7.SpringVaadinSessionListener
-
- All Implemented Interfaces:
SessionDestroyListener,SessionInitListener,Serializable
public class SpringVaadinSessionListener extends Object implements SessionInitListener, SessionDestroyListener
Manages an associated SpringWebApplicationContextwith eachVaadinSession(aka, "Vaadin application"). Typically created implicitly viaSpringVaadinServlet.Overview
In Vaadin 7, the
VaadinSessionobject holds the state associated with each client browser connection to a Vaadin servlet. For consistency with older versions of Vaadin, we'll call this a "Vaadin application" instance. This class gives each Vaadin such "Vaadin application" instance its own Spring application context, and all such application contexts share the same parent context, which is the one associated with the overal servlet web context (i.e., the one created by Spring'sContextLoaderListener). This setup is analogous to how Spring'sDispatcherServletcreates per-servlet application contexts that are children of the overall servlet web context.This class is implemented as a
SessionInitListenerandSessionDestroyListeneron the servlet'sVaadinServiceobject. In turn, the Spring context is created when a new Vaadin application instance is initialized, and destroyed when it is closed. To use this class, use theSpringVaadinServletin place of the usualVaadinServletinweb.xml.Accessing the Spring Context
The
getApplicationContext()method provides access to the application context. Alternately, use@VaadinConfigurable(see below) and implementApplicationContextAware, etc. InvokingconfigureBean()at any time will configure a bean manually.Exposing the Vaadin Session
The
VaadinSessioninstance representing the "Vaadin application" can be exposed in the associated Spring application context and therefore made available for autowiring, etc. Simply add a bean definition that invokesVaadinUtil.getCurrentSession():
This bean can then be autowired into application-specific "backend" beans, allowing them to use e.g.<bean id="vaadinSession" class="org.dellroad.stuff.vaadin7.VaadinUtil" factory-method="getCurrentSession"/>VaadinUtil.invoke(), which performs the locking necessary to avoid race conditions. But see alsoVaadinApplicationfor a convenience class that makes this process a little cleaner.@VaadinConfigurableBeansIt is also possible to configure beans outside of this application context using AOP, so that any invocation of
new FooBar(), where the classFooBaris marked@VaadinConfigurable, will automagically cause the newFooBarobject to be configured by the application context associated with the currently running Vaadin application. In effect, this does for Vaadin application beans what Spring's@Configurabledoes for regular servlet context-wide beans.Note however that Spring destroy methods will not be invoked on application close for these beans, since their lifecycle is controlled outside of the Spring application context (this is also the case with
@Configurablebeans). Instead, these beans can themselves register as aSessionDestroyListenerfor shutdown notification; but seeVaadinUtil.addSessionDestroyListener()for a memory-leak free method for doing this.Serialization and Clustering
Instances are serializable; on deserialization the
ConfigurableWebApplicationContextassociated with theVaadinSessionis refreshed; therefore, theConfigurableWebApplicationContextis not itself stored in the HTTP session by this class. This is consistent with the way normal Spring application contexts ususally work across clustered servers.However, any session-scope beans should work as expected. So while this class associates an application context with each
VaadinSession, when sessions are shared across multiple servers in a clustered environment, there will actually be a separate application contexts per server. Beans that must truly be "session wide" should be declaredscope="session"as you normally would.Note: using
scope="session"requires adding a<listener>clause registering Spring'sRequestContextListenerin yourweb.xml.
-
-
Constructor Summary
Constructors Constructor Description SpringVaadinSessionListener(String applicationName, String configLocation)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidconfigureBean(Object bean)Configure the given bean using the Spring application context associated with the current thread'sVaadinSession.static ConfigurableWebApplicationContextgetApplicationContext()Get the Spring application context associated with the current thread'sVaadinSession.static ConfigurableWebApplicationContextgetApplicationContext(VaadinSession session)Get the Spring application context associated with the givenVaadinSession.StringgetApplicationName()Get the name of this Vaadin application.protected voidloadContext(VaadinSession session, VaadinRequest request)Load the Spring application context.voidsessionDestroy(SessionDestroyEvent event)voidsessionInit(SessionInitEvent event)
-
-
-
Constructor Detail
-
SpringVaadinSessionListener
public SpringVaadinSessionListener(String applicationName, String configLocation)
Constructor.- Parameters:
applicationName- Vaadin application nameconfigLocation- XML application context config file location(s), or null for the default/WEB-INF/ServletName.xml, whereServletNameis the value ofapplicationName- Throws:
IllegalArgumentException- ifapplicationNameis null
-
-
Method Detail
-
getApplicationName
public String getApplicationName()
Get the name of this Vaadin application.- Returns:
- application name
-
getApplicationContext
public static ConfigurableWebApplicationContext getApplicationContext(VaadinSession session)
Get the Spring application context associated with the givenVaadinSession.- Parameters:
session- Vaadin session- Returns:
- Spring application context, or null if none is found
- Throws:
IllegalArgumentException- ifsessionis null
-
getApplicationContext
public static ConfigurableWebApplicationContext getApplicationContext()
Get the Spring application context associated with the current thread'sVaadinSession.- Returns:
- Spring application context, never null
- Throws:
IllegalStateException- if there is noVaadinSessionassociated with the current threadIllegalStateException- if there is no Spring application context associated with theVaadinSession
-
configureBean
public static void configureBean(Object bean)
Configure the given bean using the Spring application context associated with the current thread'sVaadinSession.- Parameters:
bean- Java bean to configure- Throws:
IllegalStateException- if there is noVaadinSessionassociated with the current threadIllegalStateException- if there is no Spring application context associated with theVaadinSession
-
sessionInit
public void sessionInit(SessionInitEvent event) throws ServiceException
- Specified by:
sessionInitin interfaceSessionInitListener- Throws:
ServiceException
-
loadContext
protected void loadContext(VaadinSession session, VaadinRequest request)
Load the Spring application context.This method expects that
sessionis already locked.- Parameters:
session- the Vaadin application sessionrequest- the triggering request
-
sessionDestroy
public void sessionDestroy(SessionDestroyEvent event)
- Specified by:
sessionDestroyin interfaceSessionDestroyListener
-
-