Class TomcatServletWebServerFactory

All Implemented Interfaces:
org.springframework.beans.factory.Aware, ConfigurableTomcatWebServerFactory, ConfigurableWebServerFactory, ErrorPageRegistry, WebServerFactory, ConfigurableServletWebServerFactory, ServletWebServerFactory, WebListenerRegistry, org.springframework.context.ResourceLoaderAware

public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory implements ConfigurableTomcatWebServerFactory, org.springframework.context.ResourceLoaderAware
AbstractServletWebServerFactory that can be used to create TomcatWebServers. Can be initialized using Spring's ServletContextInitializers or Tomcat LifecycleListeners.

Unless explicitly configured otherwise this factory will create containers that listen for HTTP requests on port 8080.

Since:
2.0.0
See Also:
  • Field Details

    • DEFAULT_PROTOCOL

      public static final String DEFAULT_PROTOCOL
      The class name of default protocol used.
      See Also:
  • Constructor Details

    • TomcatServletWebServerFactory

      public TomcatServletWebServerFactory()
      Create a new TomcatServletWebServerFactory instance.
    • TomcatServletWebServerFactory

      public TomcatServletWebServerFactory(int port)
      Create a new TomcatServletWebServerFactory that listens for requests using the specified port.
      Parameters:
      port - the port to listen on
    • TomcatServletWebServerFactory

      public TomcatServletWebServerFactory(String contextPath, int port)
      Create a new TomcatServletWebServerFactory with the specified context path and port.
      Parameters:
      contextPath - the root context path
      port - the port to listen on
  • Method Details

    • getWebServer

      public WebServer getWebServer(ServletContextInitializer... initializers)
      Description copied from interface: ServletWebServerFactory
      Gets a new fully configured but paused WebServer instance. Clients should not be able to connect to the returned server until WebServer.start() is called (which happens when the ApplicationContext has been fully refreshed).
      Specified by:
      getWebServer in interface ServletWebServerFactory
      Parameters:
      initializers - ServletContextInitializers that should be applied as the server starts
      Returns:
      a fully configured and started WebServer
      See Also:
    • prepareContext

      protected void prepareContext(org.apache.catalina.Host host, ServletContextInitializer[] initializers)
    • customizeConnector

      protected void customizeConnector(org.apache.catalina.connector.Connector connector)
    • configureContext

      protected void configureContext(org.apache.catalina.Context context, ServletContextInitializer[] initializers)
      Configure the Tomcat Context.
      Parameters:
      context - the Tomcat context
      initializers - initializers to apply
    • postProcessContext

      protected void postProcessContext(org.apache.catalina.Context context)
      Post process the Tomcat Context before it's used with the Tomcat Server. Subclasses can override this method to apply additional processing to the Context.
      Parameters:
      context - the Tomcat Context
    • getTomcatWebServer

      protected TomcatWebServer getTomcatWebServer(org.apache.catalina.startup.Tomcat tomcat)
      Factory method called to create the TomcatWebServer. Subclasses can override this method to return a different TomcatWebServer or apply additional processing to the Tomcat server.
      Parameters:
      tomcat - the Tomcat server.
      Returns:
      a new TomcatWebServer instance
    • setResourceLoader

      public void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
      Specified by:
      setResourceLoader in interface org.springframework.context.ResourceLoaderAware
    • setBaseDirectory

      public void setBaseDirectory(File baseDirectory)
      Description copied from interface: ConfigurableTomcatWebServerFactory
      Set the Tomcat base directory. If not specified a temporary directory will be used.
      Specified by:
      setBaseDirectory in interface ConfigurableTomcatWebServerFactory
      Parameters:
      baseDirectory - the tomcat base directory
    • getTldSkipPatterns

      public Set<String> getTldSkipPatterns()
      Returns a mutable set of the patterns that match jars to ignore for TLD scanning.
      Returns:
      the set of jars to ignore for TLD scanning
    • setTldSkipPatterns

      public void setTldSkipPatterns(Collection<String> patterns)
      Set the patterns that match jars to ignore for TLD scanning. See Tomcat's catalina.properties for typical values. Defaults to a list drawn from that source.
      Parameters:
      patterns - the jar patterns to skip when scanning for TLDs etc
    • addTldSkipPatterns

      public void addTldSkipPatterns(String... patterns)
      Add patterns that match jars to ignore for TLD scanning. See Tomcat's catalina.properties for typical values.
      Parameters:
      patterns - the additional jar patterns to skip when scanning for TLDs etc
    • setProtocol

      public void setProtocol(String protocol)
      The Tomcat protocol to use when create the Connector.
      Parameters:
      protocol - the protocol
      See Also:
      • Connector(String)
    • setEngineValves

      public void setEngineValves(Collection<? extends org.apache.catalina.Valve> engineValves)
      Set Valves that should be applied to the Tomcat Engine. Calling this method will replace any existing valves.
      Parameters:
      engineValves - the valves to set
    • getEngineValves

      public Collection<org.apache.catalina.Valve> getEngineValves()
      Returns a mutable collection of the Valves that will be applied to the Tomcat Engine.
      Returns:
      the engine valves that will be applied
    • addEngineValves

      public void addEngineValves(org.apache.catalina.Valve... engineValves)
      Description copied from interface: ConfigurableTomcatWebServerFactory
      Add Valves that should be applied to the Tomcat Engine.
      Specified by:
      addEngineValves in interface ConfigurableTomcatWebServerFactory
      Parameters:
      engineValves - the valves to add
    • setContextValves

      public void setContextValves(Collection<? extends org.apache.catalina.Valve> contextValves)
      Set Valves that should be applied to the Tomcat Context. Calling this method will replace any existing valves.
      Parameters:
      contextValves - the valves to set
    • getContextValves

      public Collection<org.apache.catalina.Valve> getContextValves()
      Returns a mutable collection of the Valves that will be applied to the Tomcat Context.
      Returns:
      the context valves that will be applied
      See Also:
    • addContextValves

      public void addContextValves(org.apache.catalina.Valve... contextValves)
      Add Valves that should be applied to the Tomcat Context.
      Parameters:
      contextValves - the valves to add
    • setContextLifecycleListeners

      public void setContextLifecycleListeners(Collection<? extends org.apache.catalina.LifecycleListener> contextLifecycleListeners)
      Set LifecycleListeners that should be applied to the Tomcat Context. Calling this method will replace any existing listeners.
      Parameters:
      contextLifecycleListeners - the listeners to set
    • getContextLifecycleListeners

      public Collection<org.apache.catalina.LifecycleListener> getContextLifecycleListeners()
      Returns a mutable collection of the LifecycleListeners that will be applied to the Tomcat Context.
      Returns:
      the context lifecycle listeners that will be applied
    • addContextLifecycleListeners

      public void addContextLifecycleListeners(org.apache.catalina.LifecycleListener... contextLifecycleListeners)
      Add LifecycleListeners that should be added to the Tomcat Context.
      Parameters:
      contextLifecycleListeners - the listeners to add
    • setTomcatContextCustomizers

      public void setTomcatContextCustomizers(Collection<? extends TomcatContextCustomizer> tomcatContextCustomizers)
      Set TomcatContextCustomizers that should be applied to the Tomcat Context. Calling this method will replace any existing customizers.
      Parameters:
      tomcatContextCustomizers - the customizers to set
    • getTomcatContextCustomizers

      public Collection<TomcatContextCustomizer> getTomcatContextCustomizers()
      Returns a mutable collection of the TomcatContextCustomizers that will be applied to the Tomcat Context.
      Returns:
      the listeners that will be applied
    • addContextCustomizers

      public void addContextCustomizers(TomcatContextCustomizer... tomcatContextCustomizers)
      Description copied from interface: ConfigurableTomcatWebServerFactory
      Add TomcatContextCustomizers that should be added to the Tomcat Context.
      Specified by:
      addContextCustomizers in interface ConfigurableTomcatWebServerFactory
      Parameters:
      tomcatContextCustomizers - the customizers to add
    • setTomcatConnectorCustomizers

      public void setTomcatConnectorCustomizers(Collection<? extends TomcatConnectorCustomizer> tomcatConnectorCustomizers)
      Set TomcatConnectorCustomizers that should be applied to the Tomcat Connector. Calling this method will replace any existing customizers.
      Parameters:
      tomcatConnectorCustomizers - the customizers to set
    • addConnectorCustomizers

      public void addConnectorCustomizers(TomcatConnectorCustomizer... tomcatConnectorCustomizers)
      Description copied from interface: ConfigurableTomcatWebServerFactory
      Add TomcatConnectorCustomizers that should be added to the Tomcat Connector.
      Specified by:
      addConnectorCustomizers in interface ConfigurableTomcatWebServerFactory
      Parameters:
      tomcatConnectorCustomizers - the customizers to add
    • getTomcatConnectorCustomizers

      public Collection<TomcatConnectorCustomizer> getTomcatConnectorCustomizers()
      Returns a mutable collection of the TomcatConnectorCustomizers that will be applied to the Tomcat Connector.
      Returns:
      the customizers that will be applied
    • setTomcatProtocolHandlerCustomizers

      public void setTomcatProtocolHandlerCustomizers(Collection<? extends TomcatProtocolHandlerCustomizer<?>> tomcatProtocolHandlerCustomizer)
      Set TomcatProtocolHandlerCustomizers that should be applied to the Tomcat Connector. Calling this method will replace any existing customizers.
      Parameters:
      tomcatProtocolHandlerCustomizer - the customizers to set
      Since:
      2.2.0
    • addProtocolHandlerCustomizers

      public void addProtocolHandlerCustomizers(TomcatProtocolHandlerCustomizer<?>... tomcatProtocolHandlerCustomizers)
      Add TomcatProtocolHandlerCustomizers that should be added to the Tomcat Connector.
      Specified by:
      addProtocolHandlerCustomizers in interface ConfigurableTomcatWebServerFactory
      Parameters:
      tomcatProtocolHandlerCustomizers - the customizers to add
      Since:
      2.2.0
    • getTomcatProtocolHandlerCustomizers

      public Collection<TomcatProtocolHandlerCustomizer<?>> getTomcatProtocolHandlerCustomizers()
      Returns a mutable collection of the TomcatProtocolHandlerCustomizers that will be applied to the Tomcat Connector.
      Returns:
      the customizers that will be applied
      Since:
      2.2.0
    • addAdditionalTomcatConnectors

      public void addAdditionalTomcatConnectors(org.apache.catalina.connector.Connector... connectors)
      Add Connectors in addition to the default connector, e.g. for SSL or AJP.

      Connector customizers are not applied to connectors added this way.

      Parameters:
      connectors - the connectors to add
    • getAdditionalTomcatConnectors

      public List<org.apache.catalina.connector.Connector> getAdditionalTomcatConnectors()
      Returns a mutable collection of the Connectors that will be added to the Tomcat.
      Returns:
      the additionalTomcatConnectors
    • setUriEncoding

      public void setUriEncoding(Charset uriEncoding)
      Description copied from interface: ConfigurableTomcatWebServerFactory
      Set the character encoding to use for URL decoding. If not specified 'UTF-8' will be used.
      Specified by:
      setUriEncoding in interface ConfigurableTomcatWebServerFactory
      Parameters:
      uriEncoding - the uri encoding to set
    • getUriEncoding

      public Charset getUriEncoding()
      Returns the character encoding to use for URL decoding.
      Returns:
      the URI encoding
    • setBackgroundProcessorDelay

      public void setBackgroundProcessorDelay(int delay)
      Description copied from interface: ConfigurableTomcatWebServerFactory
      Sets the background processor delay in seconds.
      Specified by:
      setBackgroundProcessorDelay in interface ConfigurableTomcatWebServerFactory
      Parameters:
      delay - the delay in seconds
    • setDisableMBeanRegistry

      public void setDisableMBeanRegistry(boolean disableMBeanRegistry)
      Set whether the factory should disable Tomcat's MBean registry prior to creating the server.
      Parameters:
      disableMBeanRegistry - whether to disable the MBean registry
      Since:
      2.2.0