Class ServletRegistrationBean<T extends javax.servlet.Servlet>
- java.lang.Object
- org.springframework.boot.web.servlet.RegistrationBean
- org.springframework.boot.web.servlet.DynamicRegistrationBean<javax.servlet.ServletRegistration.Dynamic>
- org.springframework.boot.web.servlet.ServletRegistrationBean<T>
- Type Parameters:
T
- the type of theServlet
to register
- All Implemented Interfaces:
ServletContextInitializer
,org.springframework.core.Ordered
public class ServletRegistrationBean<T extends javax.servlet.Servlet> extends DynamicRegistrationBean<javax.servlet.ServletRegistration.Dynamic>
AServletContextInitializer
to registerServlet
s in a Servlet 3.0+ container. Similar to theregistration
features provided byServletContext
but with a Spring Bean friendly design.The
servlet
must be specified before callingRegistrationBean.onStartup(javax.servlet.ServletContext)
. URL mapping can be configured usedsetUrlMappings(java.util.Collection<java.lang.String>)
or omitted when mapping to '/*' (unlessalwaysMapUrl
is set tofalse
). The servlet name will be deduced if not specified.- Since:
- 1.4.0
- See Also:
ServletContextInitializer
,ServletContext.addServlet(String, Servlet)
Constructor Summary
Constructors Constructor Description ServletRegistrationBean()
Create a newServletRegistrationBean
instance.ServletRegistrationBean(T servlet, boolean alwaysMapUrl, String... urlMappings)
Create a newServletRegistrationBean
instance with the specifiedServlet
and URL mappings.ServletRegistrationBean(T servlet, String... urlMappings)
Create a newServletRegistrationBean
instance with the specifiedServlet
and URL mappings.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected javax.servlet.ServletRegistration.Dynamic
addRegistration(String description, javax.servlet.ServletContext servletContext)
void
addUrlMappings(String... urlMappings)
Add URL mappings, as defined in the Servlet specification, for the servlet.protected void
configure(javax.servlet.ServletRegistration.Dynamic registration)
Configure registration settings.protected String
getDescription()
Return a description of the registration.javax.servlet.MultipartConfigElement
getMultipartConfig()
Returns themulti-part configuration
to be applied ornull
.T
getServlet()
Return the servlet being registered.String
getServletName()
Returns the servlet name that will be registered.Collection<String>
getUrlMappings()
Return a mutable collection of the URL mappings, as defined in the Servlet specification, for the servlet.void
setLoadOnStartup(int loadOnStartup)
Sets theloadOnStartup
priority.void
setMultipartConfig(javax.servlet.MultipartConfigElement multipartConfig)
Set themulti-part configuration
.void
setServlet(T servlet)
Sets the servlet to be registered.void
setUrlMappings(Collection<String> urlMappings)
Set the URL mappings for the servlet.String
toString()
Methods inherited from class org.springframework.boot.web.servlet.DynamicRegistrationBean
addInitParameter, getInitParameters, getOrDeduceName, isAsyncSupported, register, setAsyncSupported, setInitParameters, setName
Methods inherited from class org.springframework.boot.web.servlet.RegistrationBean
getOrder, isEnabled, onStartup, setEnabled, setOrder
Constructor Detail
ServletRegistrationBean
public ServletRegistrationBean()
Create a newServletRegistrationBean
instance.
ServletRegistrationBean
public ServletRegistrationBean(T servlet, String... urlMappings)
Create a newServletRegistrationBean
instance with the specifiedServlet
and URL mappings.- Parameters:
servlet
- the servlet being mappedurlMappings
- the URLs being mapped
ServletRegistrationBean
public ServletRegistrationBean(T servlet, boolean alwaysMapUrl, String... urlMappings)
Create a newServletRegistrationBean
instance with the specifiedServlet
and URL mappings.- Parameters:
servlet
- the servlet being mappedalwaysMapUrl
- if omitted URL mappings should be replaced with '/*'urlMappings
- the URLs being mapped
Method Detail
setServlet
public void setServlet(T servlet)
Sets the servlet to be registered.- Parameters:
servlet
- the servlet
getServlet
public T getServlet()
Return the servlet being registered.- Returns:
- the servlet
setUrlMappings
public void setUrlMappings(Collection<String> urlMappings)
Set the URL mappings for the servlet. If not specified the mapping will default to '/'. This will replace any previously specified mappings.- Parameters:
urlMappings
- the mappings to set- See Also:
addUrlMappings(String...)
getUrlMappings
public Collection<String> getUrlMappings()
Return a mutable collection of the URL mappings, as defined in the Servlet specification, for the servlet.- Returns:
- the urlMappings
addUrlMappings
public void addUrlMappings(String... urlMappings)
Add URL mappings, as defined in the Servlet specification, for the servlet.- Parameters:
urlMappings
- the mappings to add- See Also:
setUrlMappings(Collection)
setLoadOnStartup
public void setLoadOnStartup(int loadOnStartup)
Sets theloadOnStartup
priority. SeeServletRegistration.Dynamic.setLoadOnStartup(int)
for details.- Parameters:
loadOnStartup
- if load on startup is enabled
setMultipartConfig
public void setMultipartConfig(javax.servlet.MultipartConfigElement multipartConfig)
Set themulti-part configuration
.- Parameters:
multipartConfig
- the multi-part configuration to set ornull
getMultipartConfig
public javax.servlet.MultipartConfigElement getMultipartConfig()
Returns themulti-part configuration
to be applied ornull
.- Returns:
- the multipart config
getDescription
protected String getDescription()
Description copied from class:RegistrationBean
Return a description of the registration. For example "Servlet resourceServlet"- Specified by:
getDescription
in classRegistrationBean
- Returns:
- a description of the registration
addRegistration
protected javax.servlet.ServletRegistration.Dynamic addRegistration(String description, javax.servlet.ServletContext servletContext)
- Specified by:
addRegistration
in classDynamicRegistrationBean<javax.servlet.ServletRegistration.Dynamic>
configure
protected void configure(javax.servlet.ServletRegistration.Dynamic registration)
Configure registration settings. Subclasses can override this method to perform additional configuration if required.- Overrides:
configure
in classDynamicRegistrationBean<javax.servlet.ServletRegistration.Dynamic>
- Parameters:
registration
- the registration
getServletName
public String getServletName()
Returns the servlet name that will be registered.- Returns:
- the servlet name