org.springframework.boot.context.embedded
Class EmbeddedWebApplicationContext

java.lang.Object
  extended by org.springframework.core.io.DefaultResourceLoader
      extended by org.springframework.context.support.AbstractApplicationContext
          extended by org.springframework.context.support.GenericApplicationContext
              extended by org.springframework.web.context.support.GenericWebApplicationContext
                  extended by org.springframework.boot.context.embedded.EmbeddedWebApplicationContext
All Implemented Interfaces:
Closeable, org.springframework.beans.factory.BeanFactory, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.HierarchicalBeanFactory, org.springframework.beans.factory.ListableBeanFactory, org.springframework.beans.factory.support.BeanDefinitionRegistry, org.springframework.context.ApplicationContext, org.springframework.context.ApplicationEventPublisher, org.springframework.context.ConfigurableApplicationContext, org.springframework.context.Lifecycle, org.springframework.context.MessageSource, org.springframework.core.AliasRegistry, org.springframework.core.env.EnvironmentCapable, org.springframework.core.io.ResourceLoader, org.springframework.core.io.support.ResourcePatternResolver, org.springframework.ui.context.ThemeSource, org.springframework.web.context.ConfigurableWebApplicationContext, org.springframework.web.context.WebApplicationContext
Direct Known Subclasses:
AnnotationConfigEmbeddedWebApplicationContext, XmlEmbeddedWebApplicationContext

public class EmbeddedWebApplicationContext
extends org.springframework.web.context.support.GenericWebApplicationContext

A WebApplicationContext that can be used to bootstrap itself from a contained EmbeddedServletContainerFactory bean.

This context will create, initialize and run an EmbeddedServletContainer by searching for a single EmbeddedServletContainerFactory bean within the ApplicationContext itself. The EmbeddedServletContainerFactory is free to use standard Spring concepts (such as dependency injection, lifecycle callbacks and property placeholder variables).

In addition, any Servlet or Filter beans defined in the context will be automatically registered with the embedded Servlet container. In the case of a single Servlet bean, the '/' mapping will be used. If multiple Servlet beans are found then the lowercase bean name will be used as a mapping prefix. Any Servlet named 'dispatcherServlet' will always be mapped to '/'. Filter beans will be mapped to all URLs ('/*').

For more advanced configuration, the context can instead define beans that implement the ServletContextInitializer interface (most often ServletRegistrationBeans and/or FilterRegistrationBeans). To prevent double registration, the use of ServletContextInitializer beans will disable automatic Servlet and Filter bean registration.

Although this context can be used directly, most developers should consider using the AnnotationConfigEmbeddedWebApplicationContext or XmlEmbeddedWebApplicationContext variants.

Author:
Phillip Webb
See Also:
AnnotationConfigEmbeddedWebApplicationContext, XmlEmbeddedWebApplicationContext, EmbeddedServletContainerFactory

Nested Class Summary
 
Nested classes/interfaces inherited from class org.springframework.core.io.DefaultResourceLoader
org.springframework.core.io.DefaultResourceLoader.ClassPathContextResource
 
Field Summary
static String DISPATCHER_SERVLET_NAME
          Constant value for the DispatcherServlet bean name.
 
Fields inherited from class org.springframework.context.support.AbstractApplicationContext
APPLICATION_EVENT_MULTICASTER_BEAN_NAME, LIFECYCLE_PROCESSOR_BEAN_NAME, logger, MESSAGE_SOURCE_BEAN_NAME
 
Fields inherited from interface org.springframework.web.context.ConfigurableWebApplicationContext
APPLICATION_CONTEXT_ID_PREFIX, SERVLET_CONFIG_BEAN_NAME
 
Fields inherited from interface org.springframework.web.context.WebApplicationContext
CONTEXT_ATTRIBUTES_BEAN_NAME, CONTEXT_PARAMETERS_BEAN_NAME, ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, SCOPE_APPLICATION, SCOPE_GLOBAL_SESSION, SCOPE_REQUEST, SCOPE_SESSION, SERVLET_CONTEXT_BEAN_NAME
 
Fields inherited from interface org.springframework.context.ConfigurableApplicationContext
CONFIG_LOCATION_DELIMITERS, CONVERSION_SERVICE_BEAN_NAME, ENVIRONMENT_BEAN_NAME, LOAD_TIME_WEAVER_BEAN_NAME, SYSTEM_ENVIRONMENT_BEAN_NAME, SYSTEM_PROPERTIES_BEAN_NAME
 
Fields inherited from interface org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
 
Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_ALL_URL_PREFIX
 
Fields inherited from interface org.springframework.core.io.ResourceLoader
CLASSPATH_URL_PREFIX
 
Constructor Summary
EmbeddedWebApplicationContext()
           
 
Method Summary
protected  void doClose()
           
protected  void finishRefresh()
           
 EmbeddedServletContainer getEmbeddedServletContainer()
          Returns the EmbeddedServletContainer that was created by the context or null if the container has not yet been created.
protected  EmbeddedServletContainerFactory getEmbeddedServletContainerFactory()
          Returns the EmbeddedServletContainerFactory that should be used to create the embedded servlet container.
 String getNamespace()
           
protected  org.springframework.core.io.Resource getResourceByPath(String path)
           
 javax.servlet.ServletConfig getServletConfig()
           
protected  Collection<ServletContextInitializer> getServletContextInitializerBeans()
          Returns ServletContextInitializers that should be used with the embedded Servlet context.
protected  void onRefresh()
           
protected  void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
          Register ServletContextAwareProcessor.
protected  void prepareEmbeddedWebApplicationContext(javax.servlet.ServletContext servletContext)
          Prepare the WebApplicationContext with the given fully loaded ServletContext.
 void setNamespace(String namespace)
           
 void setServletConfig(javax.servlet.ServletConfig servletConfig)
           
 
Methods inherited from class org.springframework.web.context.support.GenericWebApplicationContext
createEnvironment, getApplicationName, getConfigLocations, getResourcePatternResolver, getServletContext, getTheme, initPropertySources, setConfigLocation, setConfigLocations, setServletContext
 
Methods inherited from class org.springframework.context.support.GenericApplicationContext
cancelRefresh, closeBeanFactory, getBeanDefinition, getBeanFactory, getDefaultListableBeanFactory, getResource, getResources, isAlias, isBeanNameInUse, refreshBeanFactory, registerAlias, registerBeanDefinition, removeAlias, removeBeanDefinition, setAllowBeanDefinitionOverriding, setAllowCircularReferences, setId, setParent, setResourceLoader
 
Methods inherited from class org.springframework.context.support.AbstractApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, assertBeanFactoryActive, close, containsBean, containsBeanDefinition, containsLocalBean, destroy, destroyBeans, findAnnotationOnBean, finishBeanFactoryInitialization, getAliases, getApplicationListeners, getAutowireCapableBeanFactory, getBean, getBean, getBean, getBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanFactoryPostProcessors, getBeanNamesForType, getBeanNamesForType, getBeansOfType, getBeansOfType, getBeansWithAnnotation, getDisplayName, getEnvironment, getId, getInternalParentBeanFactory, getInternalParentMessageSource, getMessage, getMessage, getMessage, getParent, getParentBeanFactory, getStartupDate, getType, initApplicationEventMulticaster, initLifecycleProcessor, initMessageSource, invokeBeanFactoryPostProcessors, isActive, isPrototype, isRunning, isSingleton, isTypeMatch, obtainFreshBeanFactory, onClose, prepareBeanFactory, prepareRefresh, publishEvent, refresh, registerBeanPostProcessors, registerListeners, registerShutdownHook, setDisplayName, setEnvironment, start, stop, toString
 
Methods inherited from class org.springframework.core.io.DefaultResourceLoader
getClassLoader, setClassLoader
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.context.ConfigurableApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, close, getBeanFactory, getEnvironment, isActive, refresh, registerShutdownHook, setEnvironment, setId, setParent
 
Methods inherited from interface org.springframework.context.ApplicationContext
getAutowireCapableBeanFactory, getDisplayName, getId, getParent, getStartupDate
 
Methods inherited from interface org.springframework.beans.factory.ListableBeanFactory
containsBeanDefinition, findAnnotationOnBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanNamesForType, getBeanNamesForType, getBeansOfType, getBeansOfType, getBeansWithAnnotation
 
Methods inherited from interface org.springframework.beans.factory.HierarchicalBeanFactory
containsLocalBean, getParentBeanFactory
 
Methods inherited from interface org.springframework.beans.factory.BeanFactory
containsBean, getAliases, getBean, getBean, getBean, getBean, getType, isPrototype, isSingleton, isTypeMatch
 
Methods inherited from interface org.springframework.context.MessageSource
getMessage, getMessage, getMessage
 
Methods inherited from interface org.springframework.context.ApplicationEventPublisher
publishEvent
 
Methods inherited from interface org.springframework.core.io.support.ResourcePatternResolver
getResources
 
Methods inherited from interface org.springframework.core.io.ResourceLoader
getClassLoader, getResource
 
Methods inherited from interface org.springframework.context.Lifecycle
isRunning, start, stop
 
Methods inherited from interface org.springframework.beans.factory.support.BeanDefinitionRegistry
containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames
 
Methods inherited from interface org.springframework.core.AliasRegistry
getAliases
 

Field Detail

DISPATCHER_SERVLET_NAME

public static final String DISPATCHER_SERVLET_NAME
Constant value for the DispatcherServlet bean name. A Servlet bean with this name is deemed to be the "main" servlet and is automatically given a mapping of "/" by default. To change the default behaviour you can use a ServletRegistrationBean or a different bean name.

See Also:
Constant Field Values
Constructor Detail

EmbeddedWebApplicationContext

public EmbeddedWebApplicationContext()
Method Detail

postProcessBeanFactory

protected void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
Register ServletContextAwareProcessor.

Overrides:
postProcessBeanFactory in class org.springframework.web.context.support.GenericWebApplicationContext
See Also:
ServletContextAwareProcessor

onRefresh

protected void onRefresh()
Overrides:
onRefresh in class org.springframework.web.context.support.GenericWebApplicationContext

finishRefresh

protected void finishRefresh()
Overrides:
finishRefresh in class org.springframework.context.support.AbstractApplicationContext

doClose

protected void doClose()
Overrides:
doClose in class org.springframework.context.support.AbstractApplicationContext

getEmbeddedServletContainerFactory

protected EmbeddedServletContainerFactory getEmbeddedServletContainerFactory()
Returns the EmbeddedServletContainerFactory that should be used to create the embedded servlet container. By default this method searches for a suitable bean in the context itself.

Returns:
a EmbeddedServletContainerFactory (never null)

getServletContextInitializerBeans

protected Collection<ServletContextInitializer> getServletContextInitializerBeans()
Returns ServletContextInitializers that should be used with the embedded Servlet context. By default this method will first attempt to find ServletContextInitializer beans, if none are found it will instead search for Servlet and Filter beans.


prepareEmbeddedWebApplicationContext

protected void prepareEmbeddedWebApplicationContext(javax.servlet.ServletContext servletContext)
Prepare the WebApplicationContext with the given fully loaded ServletContext. This method is usually called from ServletContextInitializer.onStartup(ServletContext) and is similar to the functionality usually provided by a ContextLoaderListener.

Parameters:
servletContext - the operational servlet context

getResourceByPath

protected org.springframework.core.io.Resource getResourceByPath(String path)
Overrides:
getResourceByPath in class org.springframework.web.context.support.GenericWebApplicationContext

setNamespace

public void setNamespace(String namespace)
Specified by:
setNamespace in interface org.springframework.web.context.ConfigurableWebApplicationContext
Overrides:
setNamespace in class org.springframework.web.context.support.GenericWebApplicationContext

getNamespace

public String getNamespace()
Specified by:
getNamespace in interface org.springframework.web.context.ConfigurableWebApplicationContext
Overrides:
getNamespace in class org.springframework.web.context.support.GenericWebApplicationContext

setServletConfig

public void setServletConfig(javax.servlet.ServletConfig servletConfig)
Specified by:
setServletConfig in interface org.springframework.web.context.ConfigurableWebApplicationContext
Overrides:
setServletConfig in class org.springframework.web.context.support.GenericWebApplicationContext

getServletConfig

public javax.servlet.ServletConfig getServletConfig()
Specified by:
getServletConfig in interface org.springframework.web.context.ConfigurableWebApplicationContext
Overrides:
getServletConfig in class org.springframework.web.context.support.GenericWebApplicationContext

getEmbeddedServletContainer

public EmbeddedServletContainer getEmbeddedServletContainer()
Returns the EmbeddedServletContainer that was created by the context or null if the container has not yet been created.



Copyright © 2013. All Rights Reserved.