Class ServletWebServerApplicationContext
- All Implemented Interfaces:
Closeable
,AutoCloseable
,BeanFactory
,HierarchicalBeanFactory
,ListableBeanFactory
,BeanDefinitionRegistry
,ConfigurableWebServerApplicationContext
,WebServerApplicationContext
,ApplicationContext
,ApplicationEventPublisher
,ConfigurableApplicationContext
,Lifecycle
,MessageSource
,AliasRegistry
,EnvironmentCapable
,ResourceLoader
,ResourcePatternResolver
,ThemeSource
,ConfigurableWebApplicationContext
,WebApplicationContext
- Direct Known Subclasses:
AnnotationConfigServletWebServerApplicationContext
,XmlServletWebServerApplicationContext
WebApplicationContext
that can be used to bootstrap itself from a contained
ServletWebServerFactory
bean.
This context will create, initialize and run an WebServer
by searching for a
single ServletWebServerFactory
bean within the ApplicationContext
itself. The ServletWebServerFactory
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 web server. 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
ServletRegistrationBean
s and/or FilterRegistrationBean
s). 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
AnnotationConfigServletWebServerApplicationContext
or
XmlServletWebServerApplicationContext
variants.
- Since:
- 2.0.0
- Author:
- Phillip Webb, Dave Syer, Scott Frederick
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Utility class to store and restore any user defined scopes.Nested classes/interfaces inherited from class org.springframework.core.io.DefaultResourceLoader
DefaultResourceLoader.ClassPathContextResource
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
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, MESSAGE_SOURCE_BEAN_NAME
Fields inherited from interface org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
Fields inherited from interface org.springframework.context.ConfigurableApplicationContext
APPLICATION_STARTUP_BEAN_NAME, CONFIG_LOCATION_DELIMITERS, CONVERSION_SERVICE_BEAN_NAME, ENVIRONMENT_BEAN_NAME, LOAD_TIME_WEAVER_BEAN_NAME, SHUTDOWN_HOOK_THREAD_NAME, SYSTEM_ENVIRONMENT_BEAN_NAME, SYSTEM_PROPERTIES_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.core.io.ResourceLoader
CLASSPATH_URL_PREFIX
Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_ALL_URL_PREFIX
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_REQUEST, SCOPE_SESSION, SERVLET_CONTEXT_BEAN_NAME
-
Constructor Summary
ConstructorDescriptionCreate a newServletWebServerApplicationContext
.Create a newServletWebServerApplicationContext
with the givenDefaultListableBeanFactory
. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
doClose()
protected Resource
getResourceByPath
(String path) Returns the namespace of the web server application context ornull
if no namespace has been set.protected Collection<ServletContextInitializer>
ReturnsServletContextInitializer
s that should be used with the embedded web server.Returns theWebServer
that was created by the context ornull
if the server has not yet been created.protected ServletWebServerFactory
Returns theServletWebServerFactory
that should be used to create the embeddedWebServer
.protected void
protected void
postProcessBeanFactory
(ConfigurableListableBeanFactory beanFactory) Register ServletContextAwareProcessor.protected void
prepareWebApplicationContext
(ServletContext servletContext) Prepare theWebApplicationContext
with the given fully loadedServletContext
.final void
refresh()
void
setServerNamespace
(String serverNamespace) Set the server namespace of the context.void
setServletConfig
(ServletConfig servletConfig) Methods inherited from class org.springframework.web.context.support.GenericWebApplicationContext
createEnvironment, getApplicationName, getConfigLocations, getNamespace, getResourcePatternResolver, getServletContext, getTheme, initPropertySources, setConfigLocation, setConfigLocations, setNamespace, setServletContext
Methods inherited from class org.springframework.context.support.GenericApplicationContext
cancelRefresh, closeBeanFactory, getAutowireCapableBeanFactory, getBeanDefinition, getBeanFactory, getClassLoader, getDefaultListableBeanFactory, getResource, getResources, isAlias, isBeanDefinitionOverridable, isBeanNameInUse, refreshBeanFactory, refreshForAotProcessing, registerAlias, registerBean, registerBean, registerBean, registerBean, registerBean, registerBean, registerBeanDefinition, removeAlias, removeBeanDefinition, setAllowBeanDefinitionOverriding, setAllowCircularReferences, setApplicationStartup, setClassLoader, setParent, setResourceLoader
Methods inherited from class org.springframework.context.support.AbstractApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, assertBeanFactoryActive, close, containsBean, containsBeanDefinition, containsLocalBean, destroyBeans, findAllAnnotationsOnBean, findAnnotationOnBean, findAnnotationOnBean, finishBeanFactoryInitialization, finishRefresh, getAliases, getApplicationListeners, getApplicationStartup, getBean, getBean, getBean, getBean, getBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanFactoryPostProcessors, getBeanNamesForAnnotation, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanProvider, getBeanProvider, getBeanProvider, getBeanProvider, getBeansOfType, getBeansOfType, getBeansWithAnnotation, getDisplayName, getEnvironment, getId, getInternalParentBeanFactory, getInternalParentMessageSource, getMessage, getMessage, getMessage, getParent, getParentBeanFactory, getStartupDate, getType, getType, initApplicationEventMulticaster, initLifecycleProcessor, initMessageSource, invokeBeanFactoryPostProcessors, isActive, isPrototype, isRunning, isSingleton, isTypeMatch, isTypeMatch, obtainFreshBeanFactory, onClose, prepareBeanFactory, prepareRefresh, publishEvent, publishEvent, publishEvent, registerBeanPostProcessors, registerListeners, registerShutdownHook, removeApplicationListener, resetCommonCaches, setDisplayName, setEnvironment, setId, start, stop, toString
Methods inherited from class org.springframework.core.io.DefaultResourceLoader
addProtocolResolver, clearResourceCaches, getProtocolResolvers, getResourceCache
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.springframework.core.AliasRegistry
getAliases
Methods inherited from interface org.springframework.context.ApplicationContext
getApplicationName, getAutowireCapableBeanFactory, getDisplayName, getId, getParent, getStartupDate
Methods inherited from interface org.springframework.context.ApplicationEventPublisher
publishEvent, publishEvent
Methods inherited from interface org.springframework.beans.factory.support.BeanDefinitionRegistry
containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames
Methods inherited from interface org.springframework.beans.factory.BeanFactory
containsBean, getAliases, getBean, getBean, getBean, getBean, getBean, getBeanProvider, getBeanProvider, getType, getType, isPrototype, isSingleton, isTypeMatch, isTypeMatch
Methods inherited from interface org.springframework.context.ConfigurableApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, addProtocolResolver, close, getApplicationStartup, getBeanFactory, getEnvironment, isActive, registerShutdownHook, removeApplicationListener, setApplicationStartup, setClassLoader, setEnvironment, setId, setParent
Methods inherited from interface org.springframework.beans.factory.HierarchicalBeanFactory
containsLocalBean, getParentBeanFactory
Methods inherited from interface org.springframework.beans.factory.ListableBeanFactory
containsBeanDefinition, findAllAnnotationsOnBean, findAnnotationOnBean, findAnnotationOnBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanNamesForAnnotation, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanProvider, getBeanProvider, getBeansOfType, getBeansOfType, getBeansWithAnnotation
Methods inherited from interface org.springframework.context.MessageSource
getMessage, getMessage, getMessage
Methods inherited from interface org.springframework.core.io.ResourceLoader
getClassLoader, getResource
Methods inherited from interface org.springframework.core.io.support.ResourcePatternResolver
getResources
-
Field Details
-
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 behavior you can use aServletRegistrationBean
or a different bean name.- See Also:
-
-
Constructor Details
-
ServletWebServerApplicationContext
public ServletWebServerApplicationContext()Create a newServletWebServerApplicationContext
. -
ServletWebServerApplicationContext
Create a newServletWebServerApplicationContext
with the givenDefaultListableBeanFactory
.- Parameters:
beanFactory
- the DefaultListableBeanFactory instance to use for this context
-
-
Method Details
-
postProcessBeanFactory
Register ServletContextAwareProcessor.- Overrides:
postProcessBeanFactory
in classGenericWebApplicationContext
- See Also:
-
refresh
- Specified by:
refresh
in interfaceConfigurableApplicationContext
- Overrides:
refresh
in classAbstractApplicationContext
- Throws:
BeansException
IllegalStateException
-
onRefresh
protected void onRefresh()- Overrides:
onRefresh
in classGenericWebApplicationContext
-
doClose
protected void doClose()- Overrides:
doClose
in classAbstractApplicationContext
-
getWebServerFactory
Returns theServletWebServerFactory
that should be used to create the embeddedWebServer
. By default this method searches for a suitable bean in the context itself.- Returns:
- a
ServletWebServerFactory
(nevernull
)
-
getServletContextInitializerBeans
ReturnsServletContextInitializer
s that should be used with the embedded web server. By default this method will first attempt to findServletContextInitializer
,Servlet
,Filter
and certainEventListener
beans.- Returns:
- the servlet initializer beans
-
prepareWebApplicationContext
Prepare theWebApplicationContext
with the given fully loadedServletContext
. This method is usually called fromServletContextInitializer.onStartup(ServletContext)
and is similar to the functionality usually provided by aContextLoaderListener
.- Parameters:
servletContext
- the operational servlet context
-
getResourceByPath
- Overrides:
getResourceByPath
in classGenericWebApplicationContext
-
getServerNamespace
Description copied from interface:WebServerApplicationContext
Returns the namespace of the web server application context ornull
if no namespace has been set. Used for disambiguation when multiple web servers are running in the same application (for example a management context running on a different port).- Specified by:
getServerNamespace
in interfaceWebServerApplicationContext
- Returns:
- the server namespace
-
setServerNamespace
Description copied from interface:ConfigurableWebServerApplicationContext
Set the server namespace of the context.- Specified by:
setServerNamespace
in interfaceConfigurableWebServerApplicationContext
- Parameters:
serverNamespace
- the server namespace- See Also:
-
setServletConfig
- Specified by:
setServletConfig
in interfaceConfigurableWebApplicationContext
- Overrides:
setServletConfig
in classGenericWebApplicationContext
-
getServletConfig
- Specified by:
getServletConfig
in interfaceConfigurableWebApplicationContext
- Overrides:
getServletConfig
in classGenericWebApplicationContext
-
getWebServer
Returns theWebServer
that was created by the context ornull
if the server has not yet been created.- Specified by:
getWebServer
in interfaceWebServerApplicationContext
- Returns:
- the embedded web server
-