org.dellroad.stuff.vaadin
Class SpringContextApplication

java.lang.Object
  extended by com.vaadin.Application
      extended by org.dellroad.stuff.vaadin.ContextApplication
          extended by org.dellroad.stuff.vaadin.SpringContextApplication
All Implemented Interfaces:
HttpServletRequestListener, Terminal.ErrorListener, URIHandler, Serializable

public abstract class SpringContextApplication
extends ContextApplication

Vaadin application implementation that manages an associated Spring WebApplicationContext.

Overview

Each Vaadin application instance is given 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's ContextLoaderListener). A context is created when a new Vaadin application instance is initialized, and destroyed when it is closed.

This setup is analogous to how Spring's DispatcherServlet creates per-servlet application contexts that are children of the overall servlet web context.

For each Vaadin application com.example.FooApplication that subclasses this class, there should exist an XML file named FooApplication.xml in the WEB-INF/ directory that defines the per-Vaadin application Spring application context (this naming scheme can be overriden).

Application as Bean

This SpringContextApplication instance can itself be exposed in and configured by the associated Spring application context by using a bean definition invoking the static factory method ContextApplication.get():

  <bean id="myVaadinApplication" class="org.dellroad.stuff.vaadin.ContextApplication" factory-method="get"/>
 
Note however that Spring will autowire this bean based on the type ContextApplication rather than it's actual type. To make Spring aware of your Vaadin application's actual type, you can add a custom factory method to your application class:
  public class MyVaadinApplication extends SpringContextApplication {

      public MyApplication get() {
          return ContextApplication.get(MyApplication.class);
      }
  }

  <bean id="myVaadinApplication" class="com.example.MyApplication" factory-method="get"/>
 

@VaadinConfigurable Beans

It is also possible to configure beans outside of this application context using AOP, so that any invocation of new FooBar(), where the class FooBar is marked @VaadinConfigurable, will automagically cause the new FooBar object to be configured by the application context associated with the currently running application instance. In effect, this does for Vaadin application beans what Spring's @Configurable does for regular 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 @Configurable beans). Instead, these beans can register as a ContextApplication.CloseListener for shutdown notification.

For the this annotation to do anything, @VaadinConfigurable classes must be woven (either at build time or runtime) using the AspectJ compiler with the VaadinConfigurableAspect aspect (included in the dellroad-stuff JAR file).

See Also:
ContextApplication.get(), ContextApplicationFactoryBean, Example Code on GitHub, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class org.dellroad.stuff.vaadin.ContextApplication
ContextApplication.CloseEvent, ContextApplication.CloseListener
 
Nested classes/interfaces inherited from class com.vaadin.Application
Application.ApplicationError, Application.CustomizedSystemMessages, Application.SystemMessages, Application.UserChangeEvent, Application.UserChangeListener, Application.WindowAttachEvent, Application.WindowAttachListener, Application.WindowDetachEvent, Application.WindowDetachListener
 
Nested classes/interfaces inherited from interface com.vaadin.terminal.URIHandler
URIHandler.ErrorEvent
 
Field Summary
 
Fields inherited from class org.dellroad.stuff.vaadin.ContextApplication
DEFAULT_NOTIFICATION_DELAY, log
 
Constructor Summary
SpringContextApplication()
           
 
Method Summary
protected  void destroySpringApplication()
          Perform any application-specific shutdown work.
static SpringContextApplication get()
          Get the SpringContextApplication instance associated with the current thread or throw an exception if there is none.
 ConfigurableWebApplicationContext getApplicationContext()
          Get this instance's associated Spring application context.
protected  String getApplicationName()
          Get the name for this application.
protected  void initApplication()
          Initializes the associated ConfigurableWebApplicationContext.
protected abstract  void initSpringApplication(ConfigurableWebApplicationContext context)
          Initialize the application.
protected  void onRefresh(ApplicationContext context)
          Perform any application-specific work after a successful application context refresh.
protected  void postProcessWebApplicationContext(ConfigurableWebApplicationContext context)
          Post-process the given WebApplicationContext after initial creation but before the initial refresh().
 
Methods inherited from class org.dellroad.stuff.vaadin.ContextApplication
addListener, close, currentApplication, currentApplication, currentRequest, currentResponse, doOnRequestEnd, doOnRequestStart, get, getErrorMessage, getNotificationDelay, init, invoke, invokeLater, onRequestEnd, onRequestStart, removeListener, showError, showError, terminalError
 
Methods inherited from class com.vaadin.Application
addListener, addListener, addListener, addResource, addWindow, getContext, getErrorHandler, getLocale, getLogoutURL, getMainWindow, getProperty, getPropertyNames, getRelativeLocation, getSystemMessages, getTheme, getURL, getUser, getVersion, getWindow, getWindows, handleURI, isRunning, removeListener, removeListener, removeListener, removeResource, removeWindow, setErrorHandler, setLocale, setLogoutURL, setMainWindow, setTheme, setUser, start
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpringContextApplication

public SpringContextApplication()
Method Detail

getApplicationContext

public ConfigurableWebApplicationContext getApplicationContext()
Get this instance's associated Spring application context.


get

public static SpringContextApplication get()
Get the SpringContextApplication instance associated with the current thread or throw an exception if there is none.

Works just like ContextApplication.get() but returns this narrower type.

Returns:
the SpringContextApplication associated with the current thread
Throws:
IllegalStateException - if the current thread is not servicing a Vaadin web request or the current Vaadin Application is not a SpringContextApplication

initApplication

protected final void initApplication()
Initializes the associated ConfigurableWebApplicationContext.

Specified by:
initApplication in class ContextApplication

initSpringApplication

protected abstract void initSpringApplication(ConfigurableWebApplicationContext context)
Initialize the application. Sub-classes of SpringContextApplication must implement this method.

Parameters:
context - the associated WebApplicationContext just created and refreshed
See Also:
destroySpringApplication()

destroySpringApplication

protected void destroySpringApplication()
Perform any application-specific shutdown work. This will be invoked at shutdown after this Vaadin application and the associated WebApplicationContext have both been closed.

The implementation in SpringContextApplication does nothing. Subclasses may override as necessary.

Note that if a SpringContextApplication instance is exposed in the application context and configured with a Spring destroy method, then that method will also be invoked when the application is closed. In such cases overriding this method is not necessary.

See Also:
initSpringApplication(org.springframework.web.context.ConfigurableWebApplicationContext)

postProcessWebApplicationContext

protected void postProcessWebApplicationContext(ConfigurableWebApplicationContext context)
Post-process the given WebApplicationContext after initial creation but before the initial refresh().

The implementation in SpringContextApplication does nothing. Subclasses may override as necessary.

Parameters:
context - the associated WebApplicationContext just refreshed
See Also:
onRefresh(org.springframework.context.ApplicationContext), ConfigurableApplicationContext.refresh()

onRefresh

protected void onRefresh(ApplicationContext context)
Perform any application-specific work after a successful application context refresh.

The implementation in SpringContextApplication does nothing. Subclasses may override as necessary.

Parameters:
context - the associated WebApplicationContext just refreshed
See Also:
postProcessWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext), ConfigurableApplicationContext.refresh()

getApplicationName

protected String getApplicationName()
Get the name for this application. This is used as the name of the XML file in WEB-INF/ that defines the Spring application context associated with this instance.

The implementation in SpringContextApplication returns this instance's class' simple name.