Spring Boot中Tomcat是怎么启动的 (2)

大家如果看Spring Boot的源代码,这边有个小技巧大家可以参考下。就是Spring Boot把之前传统项目中的配置项都通过AutoConfig的形式
做配置了。所以这边在寻找DispatcherServlet是在哪里配置的也可以顺着这个思路去寻找。

在IDEA的类查找功能中输入DispatcherServlet关键字,我们能看到一个DispatcherServletAutoConfiguration类。从名字上就能看出这个
类是DispatcherServlet的自动配置类,我们点进去看下是否是在这个类内部注册的DispatcherServlet?

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) @Configuration @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass(DispatcherServlet.class) @AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class) @EnableConfigurationProperties(ServerProperties.class) public class DispatcherServletAutoConfiguration { /* * The bean name for a DispatcherServlet that will be mapped to the root URL "http://www.likecs.com/" */ public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet"; /* * The bean name for a ServletRegistrationBean for the DispatcherServlet "http://www.likecs.com/" */ public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration"; @Configuration @Conditional(DefaultDispatcherServletCondition.class) @ConditionalOnClass(ServletRegistration.class) @EnableConfigurationProperties(WebMvcProperties.class) protected static class DispatcherServletConfiguration { private final WebMvcProperties webMvcProperties; public DispatcherServletConfiguration(WebMvcProperties webMvcProperties) { this.webMvcProperties = webMvcProperties; } @Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public DispatcherServlet dispatcherServlet() { DispatcherServlet dispatcherServlet = new DispatcherServlet(); dispatcherServlet.setDispatchOptionsRequest( this.webMvcProperties.isDispatchOptionsRequest()); dispatcherServlet.setDispatchTraceRequest( this.webMvcProperties.isDispatchTraceRequest()); dispatcherServlet.setThrowExceptionIfNoHandlerFound( this.webMvcProperties.isThrowExceptionIfNoHandlerFound()); return dispatcherServlet; } @Bean @ConditionalOnBean(MultipartResolver.class) @ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME) public MultipartResolver multipartResolver(MultipartResolver resolver) { // Detect if the user has created a MultipartResolver but named it incorrectly return resolver; } } @Configuration @Conditional(DispatcherServletRegistrationCondition.class) @ConditionalOnClass(ServletRegistration.class) @EnableConfigurationProperties(WebMvcProperties.class) @Import(DispatcherServletConfiguration.class) protected static class DispatcherServletRegistrationConfiguration { private final ServerProperties serverProperties; private final WebMvcProperties webMvcProperties; private final MultipartConfigElement multipartConfig; public DispatcherServletRegistrationConfiguration( ServerProperties serverProperties, WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfigProvider) { this.serverProperties = serverProperties; this.webMvcProperties = webMvcProperties; this.multipartConfig = multipartConfigProvider.getIfAvailable(); } //很熟悉的代码有没有,ServletRegistrationBean就是我们上一节中介绍的注册Servlet的方式 //只不过这边注册的是DispatcherServlet这个特殊的Servlet @Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME) @ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public ServletRegistrationBean<DispatcherServlet> dispatcherServletRegistration( DispatcherServlet dispatcherServlet) { ServletRegistrationBean<DispatcherServlet> registration = new ServletRegistrationBean<>( dispatcherServlet, this.serverProperties.getServlet().getServletMapping()); registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); registration.setLoadOnStartup( this.webMvcProperties.getServlet().getLoadOnStartup()); if (this.multipartConfig != null) { registration.setMultipartConfig(this.multipartConfig); } return registration; } } //...省略部分代码 }

好了通过这边的介绍,我们直到DispatcherServlet是通过DispatcherServletAutoConfiguration这个自动配置类注册的。

Spring Boot中关于Tomcat的一些其他配置 server.tomcat.accept-count 100.0 Maximum queue length for incoming connection requests when all possible request processing threads are in use.(backlog的长度)
server.tomcat.accesslog.buffered   true   Whether to buffer output such that it is flushed only periodically.  
server.tomcat.accesslog.check-exists   false   Whether to check for log file existence so it can be recreated it if an external process has renamed it.  
server.tomcat.accesslog.condition-if     Whether logging of the request will only be enabled if "ServletRequest.getAttribute(conditionIf)" does not yield null.  
server.tomcat.accesslog.condition-unless     Whether logging of the request will only be enabled if "ServletRequest.getAttribute(conditionUnless)" yield null.  
server.tomcat.accesslog.directory   logs   Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.  
server.tomcat.accesslog.enabled   false   Enable access log.  
server.tomcat.accesslog.encoding     Character set used by the log file. Default to the system default character set.  
server.tomcat.accesslog.file-date-format   .yyyy-MM-dd   Date format to place in the log file name.  
server.tomcat.accesslog.ipv6-canonical   false   Whether to use IPv6 canonical representation format as defined by RFC 5952.  
server.tomcat.accesslog.locale     Locale used to format timestamps in log entries and in log file name suffix. Default to the default locale of the Java process.  
server.tomcat.accesslog.max-days   -1.0   Number of days to retain the access log files before they are removed.  
server.tomcat.accesslog.pattern   common   Format pattern for access logs.  
server.tomcat.accesslog.prefix   access_log   Log file name prefix.  
server.tomcat.accesslog.rename-on-rotate   false   Whether to defer inclusion of the date stamp in the file name until rotate time.  
server.tomcat.accesslog.request-attributes-enabled   false   Set request attributes for the IP address, Hostname, protocol, and port used for the request.  
server.tomcat.accesslog.rotate   true   Whether to enable access log rotation.  
server.tomcat.accesslog.suffix   .log   Log file name suffix.  
server.tomcat.additional-tld-skip-patterns     Comma-separated list of additional patterns that match jars to ignore for TLD scanning. The special '?' and '*' characters can be used in the pattern to match one and only one character and zero or more characters respectively.  
server.tomcat.background-processor-delay   10s   Delay between the invocation of backgroundProcess methods. If a duration suffix is not specified, seconds will be used.  
server.tomcat.basedir     Tomcat base directory. If not specified, a temporary directory is used.  
server.tomcat.connection-timeout     Amount of time the connector will wait, after accepting a connection, for the request URI line to be presented.  
server.tomcat.max-connections   8192.0   Maximum number of connections that the server accepts and processes at any given time. Once the limit has been reached, the operating system may still accept connections based on the "acceptCount" property.  
server.tomcat.max-http-form-post-size   2MB   Maximum size of the form content in any HTTP post request.  
server.tomcat.max-swallow-size   2MB   Maximum amount of request body to swallow.  
server.tomcat.mbeanregistry.enabled   false   Whether Tomcat's MBean Registry should be enabled.  
server.tomcat.processor-cache   200.0   Maximum number of idle processors that will be retained in the cache and reused with a subsequent request. When set to -1 the cache will be unlimited with a theoretical maximum size equal to the maximum number of connections.  
server.tomcat.redirect-context-root   true   Whether requests to the context root should be redirected by appending a / to the path.  
server.tomcat.relaxed-path-chars     Comma-separated list of additional unencoded characters that should be allowed in URI paths. Only "< > [ \ ] ^ ` { | }" are allowed.  
server.tomcat.relaxed-query-chars     Comma-separated list of additional unencoded characters that should be allowed in URI query strings. Only "< > [ \ ] ^ ` { | }" are allowed.  
server.tomcat.remoteip.host-header   X-Forwarded-Host   Name of the HTTP header from which the remote host is extracted.  
server.tomcat.remoteip.internal-proxies     Regular expression that matches proxies that are to be trusted.  
server.tomcat.remoteip.port-header   X-Forwarded-Port   Name of the HTTP header used to override the original port value.  
server.tomcat.remoteip.protocol-header     Header that holds the incoming protocol, usually named "X-Forwarded-Proto".  
server.tomcat.remoteip.protocol-header-https-value   https   Value of the protocol header indicating whether the incoming request uses SSL.  
server.tomcat.remoteip.remote-ip-header     Name of the HTTP header from which the remote IP is extracted. For instance, X-FORWARDED-FOR.  
server.tomcat.resource.allow-caching   true   Whether static resource caching is permitted for this web application.  
server.tomcat.resource.cache-ttl     Time-to-live of the static resource cache.  
server.tomcat.threads.max   200.0   Maximum amount of worker threads.  
server.tomcat.threads.min-spare   10.0   Minimum amount of worker threads.  
server.tomcat.uri-encoding   UTF-8   Character encoding to use to decode the URI.  
server.tomcat.use-relative-redirects   false   Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsxwxf.html