SpringBoot内嵌的Tomcat启动过程以及请求

1.springboot内嵌的tomcat的pom坐标

 启动后可以看到tomcat版本为9.0.46

 2.springboot 内嵌tomcat启动流程

点击进入SpringApplication.run()方法里面

看这次tomcat启动相关的核心代码refreshContext(context);刷新上下文方法

public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch = new StopWatch();stopWatch.start();ConfigurableApplicationContext context = null;configureHeadlessProperty();SpringApplicationRunListeners listeners = getRunListeners(args);listeners.starting();try {ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);configureIgnoreBeanInfo(environment);Banner printedBanner = printBanner(environment);context = createApplicationContext();prepareContext(context, environment, listeners, applicationArguments, printedBanner);// 此次的核心代码是刷新上下文方法refreshContext(context);afterRefresh(context, applicationArguments);stopWatch.stop();if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);}listeners.started(context);callRunners(context, applicationArguments);}catch (Throwable ex) {handleRunFailure(context, ex, listeners);throw new IllegalStateException(ex);}try {listeners.running(context);}catch (Throwable ex) {handleRunFailure(context, ex, null);throw new IllegalStateException(ex);}return context;
}

进入 私有方法refreshContext

private void refreshContext(ConfigurableApplicationContext context) {if (this.registerShutdownHook) {try {context.registerShutdownHook();}catch (AccessControlException ex) {// Not allowed in some environments.}}// 核心方法,继续进入refresh((ApplicationContext) context);
}
@Deprecated
protected void refresh(ApplicationContext applicationContext) {Assert.isInstanceOf(ConfigurableApplicationContext.class, applicationContext);// 核心代码,继续进入方法refresh((ConfigurableApplicationContext) applicationContext);
}

进入refresh方法

protected void refresh(ConfigurableApplicationContext applicationContext) {// 核心方法,查看调用的方法refresh()applicationContext.refresh();
}

查看refresh方法,有三个实现类,进入默认的实现类ServletWebServerApplicationContext,定位到实现方法

@Override
public final void refresh() throws BeansException, IllegalStateException {try {// 进入父类refreshsuper.refresh();}catch (RuntimeException ex) {WebServer webServer = this.webServer;if (webServer != null) {webServer.stop();}throw ex;}
}

 看onRefresh()方法

@Override
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.prepareRefresh();// Tell the subclass to refresh the internal bean factory.ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.registerBeanPostProcessors(beanFactory);// Initialize message source for this context.initMessageSource();// Initialize event multicaster for this context.initApplicationEventMulticaster();// 我们进入这个方法// Initialize other special beans in specific context subclasses.onRefresh();// Check for listener beans and register them.registerListeners();// Instantiate all remaining (non-lazy-init) singletons.finishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.finishRefresh();}catch (BeansException ex) {if (logger.isWarnEnabled()) {logger.warn("Exception encountered during context initialization - " +"cancelling refresh attempt: " + ex);}// Destroy already created singletons to avoid dangling resources.destroyBeans();// Reset 'active' flag.cancelRefresh(ex);// Propagate exception to caller.throw ex;}finally {// Reset common introspection caches in Spring's core, since we// might not ever need metadata for singleton beans anymore...resetCommonCaches();}}
}

 找到子类的实现方法:

@Override
protected void onRefresh() {super.onRefresh();try {// 核心代码,创建web服务器createWebServer();}catch (Throwable ex) {throw new ApplicationContextException("Unable to start web server", ex);}
}

进入createWebServer方法,ServletContext、WebServer在tomcat启动的时候一定为空,还没有初始化完成,会进入if代码块执行

private void createWebServer() {WebServer webServer = this.webServer;// tomcat启动servletContext才进行创建ServletContext servletContext = getServletContext();if (webServer == null && servletContext == null) {ServletWebServerFactory factory = getWebServerFactory();// 核心代码,通过工厂获取一个WebServer实例this.webServer = factory.getWebServer(getSelfInitializer());getBeanFactory().registerSingleton("webServerGracefulShutdown",new WebServerGracefulShutdownLifecycle(this.webServer));getBeanFactory().registerSingleton("webServerStartStop",new WebServerStartStopLifecycle(this, this.webServer));}else if (servletContext != null) {try {getSelfInitializer().onStartup(servletContext);}catch (ServletException ex) {throw new ApplicationContextException("Cannot initialize servlet context", ex);}}initPropertySources();
}

 getWebServer方法需要参数,该参数是一个方法getSelfInitializer(),找到

private org.springframework.boot.web.servlet.ServletContextInitializer getSelfInitializer() {// 方法引用,ServletContextInitializer是一个函数是接口,通过方法引用返回一个实现        // ServletContextInitializer的一个实现子类,作用同匿名内部类。return this::selfInitialize;
}

函数式接口:

@FunctionalInterface
public interface ServletContextInitializer {/*** Configure the given {@link ServletContext} with any servlets, filters, listeners* context-params and attributes necessary for initialization.* @param servletContext the {@code ServletContext} to initialize* @throws ServletException if any call against the given {@code ServletContext}* throws a {@code ServletException}*/void onStartup(ServletContext servletContext) throws ServletException;}

this::selfInitialize就是实例::方法名调用方式,this代表当前对象,selfInitialize是this(ServletWebServerApplicationContext)对象的一个私有方法

private void selfInitialize(ServletContext servletContext) throws ServletException {prepareWebApplicationContext(servletContext);registerApplicationScope(servletContext);WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(), servletContext);for (ServletContextInitializer beans : getServletContextInitializerBeans()) {beans.onStartup(servletContext);}
}

继续进入getWebServer,发现是一个接口,有三个实现类Jetty、Tomcat、UnderTow,我们是Tomcat服务器,进入TomcatServletWebServerFactory中的实现方法

@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {if (this.disableMBeanRegistry) {Registry.disableRegistry();}Tomcat tomcat = new Tomcat();File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");// 用于设置临时文件的目录,这些目录用于存放JSP生成的源代码及Class文件xtomcat.setBaseDir(baseDir.getAbsolutePath());Connector connector = new Connector(this.protocol);connector.setThrowOnFailure(true);tomcat.getService().addConnector(connector);customizeConnector(connector);// 用于设置链接器,包括协议、I/0、端口、压缩、加密等等tomcat.setConnector(connector);// 是否自动部署tomcat.getHost().setAutoDeploy(false);configureEngine(tomcat.getEngine());for (Connector additionalConnector : this.additionalTomcatConnectors) {tomcat.getService().addConnector(additionalConnector);}prepareContext(tomcat.getHost(), initializers);// 核心代码return getTomcatWebServer(tomcat);
}
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {return new TomcatWebServer(tomcat, getPort() >= 0, getShutdown());
}

进入TomcatWebServer构造方法

public TomcatWebServer(Tomcat tomcat, boolean autoStart, Shutdown shutdown) {Assert.notNull(tomcat, "Tomcat Server must not be null");this.tomcat = tomcat;this.autoStart = autoStart;this.gracefulShutdown = (shutdown == Shutdown.GRACEFUL) ? new GracefulShutdown(tomcat) : null;// 核心代码initialize();
}

 进入initialize()方法

private void initialize() throws WebServerException {logger.info("Tomcat initialized with port(s): " + getPortsDescription(false));synchronized (this.monitor) {try {addInstanceIdToEngineName();Context context = findContext();context.addLifecycleListener((event) -> {if (context.equals(event.getSource()) && Lifecycle.START_EVENT.equals(event.getType())) {// Remove service connectors so that protocol binding doesn't// happen when the service is started.removeServiceConnectors();}});// 核心代码// Start the server to trigger initialization listenersthis.tomcat.start();// We can re-throw failure exception directly in the main threadrethrowDeferredStartupExceptions();try {ContextBindings.bindClassLoader(context, context.getNamingToken(), getClass().getClassLoader());}catch (NamingException ex) {// Naming is not enabled. Continue}// Unlike Jetty, all Tomcat threads are daemon threads. We create a// blocking non-daemon to stop immediate shutdownstartDaemonAwaitThread();}catch (Exception ex) {stopSilently();destroySilently();throw new WebServerException("Unable to start embedded Tomcat", ex);}}
}
public void start() throws LifecycleException {getServer();// 核心代码server.start();
}

进入start()方法,找到LifecycleBase实现类方法

 LifecycleBase类中init();startInternal();两个核心方法

 @Override
public final synchronized void start() throws LifecycleException {if (LifecycleState.STARTING_PREP.equals(state) || LifecycleState.STARTING.equals(state) ||LifecycleState.STARTED.equals(state)) {if (log.isDebugEnabled()) {Exception e = new LifecycleException();log.debug(sm.getString("lifecycleBase.alreadyStarted", toString()), e);} else if (log.isInfoEnabled()) {log.info(sm.getString("lifecycleBase.alreadyStarted", toString()));}return;}if (state.equals(LifecycleState.NEW)) {// 核心代码1init();} else if (state.equals(LifecycleState.FAILED)) {stop();} else if (!state.equals(LifecycleState.INITIALIZED) &&!state.equals(LifecycleState.STOPPED)) {invalidTransition(Lifecycle.BEFORE_START_EVENT);}try {setStateInternal(LifecycleState.STARTING_PREP, null, false);// 核心代码2startInternal();if (state.equals(LifecycleState.FAILED)) {// This is a 'controlled' failure. The component put itself into the// FAILED state so call stop() to complete the clean-up.stop();} else if (!state.equals(LifecycleState.STARTING)) {// Shouldn't be necessary but acts as a check that sub-classes are// doing what they are supposed to.invalidTransition(Lifecycle.AFTER_START_EVENT);} else {setStateInternal(LifecycleState.STARTED, null, false);}} catch (Throwable t) {// This is an 'uncontrolled' failure so put the component into the// FAILED state and throw an exception.handleSubClassException(t, "lifecycleBase.startFail", toString());}
}

 init()方法

 @Override
public final synchronized void init() throws LifecycleException {if (!state.equals(LifecycleState.NEW)) {invalidTransition(Lifecycle.BEFORE_INIT_EVENT);}try {setStateInternal(LifecycleState.INITIALIZING, null, false);// 核心代码initInternal();setStateInternal(LifecycleState.INITIALIZED, null, false);} catch (Throwable t) {handleSubClassException(t, "lifecycleBase.initFail", toString());}
}

initInternal有很多实现类,看组件Connector中的实现方法

 组件Connector类型方法

@Override
protected void initInternal() throws LifecycleException {super.initInternal();if (protocolHandler == null) {throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerInstantiationFailed"));}// Initialize adapteradapter = new CoyoteAdapter(this);protocolHandler.setAdapter(adapter);if (service != null) {protocolHandler.setUtilityExecutor(service.getServer().getUtilityExecutor());}// Make sure parseBodyMethodsSet has a defaultif (null == parseBodyMethodsSet) {setParseBodyMethods(getParseBodyMethods());}if (protocolHandler.isAprRequired() && !AprStatus.isInstanceCreated()) {throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerNoAprListener",getProtocolHandlerClassName()));}if (protocolHandler.isAprRequired() && !AprStatus.isAprAvailable()) {throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerNoAprLibrary",getProtocolHandlerClassName()));}if (AprStatus.isAprAvailable() && AprStatus.getUseOpenSSL() &&protocolHandler instanceof AbstractHttp11JsseProtocol) {AbstractHttp11JsseProtocol<?> jsseProtocolHandler =(AbstractHttp11JsseProtocol<?>) protocolHandler;if (jsseProtocolHandler.isSSLEnabled() &&jsseProtocolHandler.getSslImplementationName() == null) {// OpenSSL is compatible with the JSSE configuration, so use it if APR is availablejsseProtocolHandler.setSslImplementationName(OpenSSLImplementation.class.getName());}}try {// 核心方法protocolHandler.init();} catch (Exception e) {throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerInitializationFailed"), e);}
}

进入实现类AbstractProtocol中init()方法

@Override
public void init() throws Exception {if (getLog().isInfoEnabled()) {getLog().info(sm.getString("abstractProtocolHandler.init", getName()));logPortOffset();}if (oname == null) {// Component not pre-registered so register itoname = createObjectName();if (oname != null) {Registry.getRegistry(null, null).registerComponent(this, oname, null);}}if (this.domain != null) {ObjectName rgOname = new ObjectName(domain + ":type=GlobalRequestProcessor,name=" + getName());this.rgOname = rgOname;Registry.getRegistry(null, null).registerComponent(getHandler().getGlobal(), rgOname, null);}String endpointName = getName();endpoint.setName(endpointName.substring(1, endpointName.length()-1));endpoint.setDomain(domain);// 核心代码endpoint.init();
}

进入endpoint.init()方法

public final void init() throws Exception {if (bindOnInit) {// 核心代码bindWithCleanup();bindState = BindState.BOUND_ON_INIT;}if (this.domain != null) {// Register endpoint (as ThreadPool - historical name)oname = new ObjectName(domain + ":type=ThreadPool,name=\"" + getName() + "\"");Registry.getRegistry(null, null).registerComponent(this, oname, null);ObjectName socketPropertiesOname = new ObjectName(domain +":type=SocketProperties,name=\"" + getName() + "\"");socketProperties.setObjectName(socketPropertiesOname);Registry.getRegistry(null, null).registerComponent(socketProperties, socketPropertiesOname, null);for (SSLHostConfig sslHostConfig : findSslHostConfigs()) {registerJmx(sslHostConfig);}}
}

进入bindWithCleanup方法里面 

private void bindWithCleanup() throws Exception {try {// 核心代码bind();} catch (Throwable t) {// Ensure open sockets etc. are cleaned up if something goes// wrong during bindExceptionUtils.handleThrowable(t);unbind();throw t;}
}
@Override
public void bind() throws Exception {// 核心代码initServerSocket();setStopLatch(new CountDownLatch(1));// Initialize SSL if neededinitialiseSsl();selectorPool.open(getName());
}

初始化Socket 

protected void initServerSocket() throws Exception {if (getUseInheritedChannel()) {// Retrieve the channel provided by the OSChannel ic = System.inheritedChannel();if (ic instanceof ServerSocketChannel) {serverSock = (ServerSocketChannel) ic;}if (serverSock == null) {throw new IllegalArgumentException(sm.getString("endpoint.init.bind.inherited"));}} else if (getUnixDomainSocketPath() != null) {SocketAddress sa = JreCompat.getInstance().getUnixDomainSocketAddress(getUnixDomainSocketPath());serverSock = JreCompat.getInstance().openUnixDomainServerSocketChannel();serverSock.bind(sa, getAcceptCount());if (getUnixDomainSocketPathPermissions() != null) {Path path = Paths.get(getUnixDomainSocketPath());Set<PosixFilePermission> permissions =PosixFilePermissions.fromString(getUnixDomainSocketPathPermissions());if (path.getFileSystem().supportedFileAttributeViews().contains("posix")) {FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(permissions);Files.setAttribute(path, attrs.name(), attrs.value());} else {java.io.File file = path.toFile();if (permissions.contains(PosixFilePermission.OTHERS_READ) && !file.setReadable(true, false)) {log.warn(sm.getString("endpoint.nio.perms.readFail", file.getPath()));}if (permissions.contains(PosixFilePermission.OTHERS_WRITE) && !file.setWritable(true, false)) {log.warn(sm.getString("endpoint.nio.perms.writeFail", file.getPath()));}}}} else {serverSock = ServerSocketChannel.open();socketProperties.setProperties(serverSock.socket());InetSocketAddress addr = new InetSocketAddress(getAddress(), getPortWithOffset());serverSock.bind(addr, getAcceptCount());}serverSock.configureBlocking(true); //mimic APR behavior
}

startInternal();

看下这个方法,找到Connector里面的startInternal()方法实现

@Override
protected void startInternal() throws LifecycleException {// Validate settings before startingString id = (protocolHandler != null) ? protocolHandler.getId() : null;if (id == null && getPortWithOffset() < 0) {throw new LifecycleException(sm.getString("coyoteConnector.invalidPort", Integer.valueOf(getPortWithOffset())));}setState(LifecycleState.STARTING);try {// 核心代码protocolHandler.start();} catch (Exception e) {throw new LifecycleException(sm.getString("coyoteConnector.protocolHandlerStartFailed"), e);}
}

进入protocolHandler.start();方法里面。进入AbstractProtocol里面的实现方法

@Override
public void start() throws Exception {if (getLog().isInfoEnabled()) {getLog().info(sm.getString("abstractProtocolHandler.start", getName()));logPortOffset();}// 核心代码endpoint.start();monitorFuture = getUtilityExecutor().scheduleWithFixedDelay(() -> {if (!isPaused()) {startAsyncTimeout();}}, 0, 60, TimeUnit.SECONDS);
}

找到endpoint.start()方法

public final void start() throws Exception {if (bindState == BindState.UNBOUND) {bindWithCleanup();bindState = BindState.BOUND_ON_START;}// 核心代码startInternal();
}

进入这个实现类里,找到实现方法

 

@Overridepublic void startInternal() throws Exception {if (!running) {running = true;paused = false;if (socketProperties.getProcessorCache() != 0) {processorCache = new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,socketProperties.getProcessorCache());}if (socketProperties.getEventCache() != 0) {eventCache = new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,socketProperties.getEventCache());}if (socketProperties.getBufferPool() != 0) {nioChannels = new SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,socketProperties.getBufferPool());}// Create worker collectionif (getExecutor() == null) {createExecutor();}initializeConnectionLatch();// 核心代码 *******// Start poller threadpoller = new Poller();Thread pollerThread = new Thread(poller, getName() + "-ClientPoller");// 设置进程的优先级pollerThread.setPriority(threadPriority);// 守护线程:当非守护线程销毁的时候,守护线程跟着销毁。当运行的唯一线程是守护线程时,        // Java虚拟机将退出pollerThread.setDaemon(true);// 开启线程pollerThread.start();startAcceptorThread();}}

Poller类是什么?

Poller里面的run方法

@Override
public void run() {// Loop until destroy() is called// 核心代码1 轮询while (true) {boolean hasEvents = false;try {if (!close) {hasEvents = events();if (wakeupCounter.getAndSet(-1) > 0) {// If we are here, means we have other stuff to do// Do a non blocking selectkeyCount = selector.selectNow();} else {keyCount = selector.select(selectorTimeout);}wakeupCounter.set(0);}if (close) {events();timeout(0, false);try {selector.close();} catch (IOException ioe) {log.error(sm.getString("endpoint.nio.selectorCloseFail"), ioe);}break;}// Either we timed out or we woke up, process events firstif (keyCount == 0) {hasEvents = (hasEvents | events());}} catch (Throwable x) {ExceptionUtils.handleThrowable(x);log.error(sm.getString("endpoint.nio.selectorLoopError"), x);continue;}Iterator<SelectionKey> iterator =keyCount > 0 ? selector.selectedKeys().iterator() : null;// Walk through the collection of ready keys and dispatch// any active event.while (iterator != null && iterator.hasNext()) {SelectionKey sk = iterator.next();iterator.remove();NioSocketWrapper socketWrapper = (NioSocketWrapper) sk.attachment();// Attachment may be null if another thread has called// cancelledKey()if (socketWrapper != null) {// 核心代码2processKey(sk, socketWrapper);}}// Process timeoutstimeout(keyCount,hasEvents);}getStopLatch().countDown();
}

启动一个Poller线程轮询监听NIO接收的请求,直到执行核心代码2processKey(sk, socketWrapper);Poller类会轮询监听Socket连接,到这里tomcat算启动成功啦。

springboot嵌入tomcat处理请求的过程

主要看下Poller类中的processKey方法,我们发起一个请求怎么处理的呢?

processKey方法

protected void processKey(SelectionKey sk, NioSocketWrapper socketWrapper) {try {if (close) {cancelledKey(sk, socketWrapper);} else if (sk.isValid() && socketWrapper != null) {if (sk.isReadable() || sk.isWritable()) {if (socketWrapper.getSendfileData() != null) {processSendfile(sk, socketWrapper, false);} else {unreg(sk, socketWrapper, sk.readyOps());boolean closeSocket = false;// Read goes before writeif (sk.isReadable()) {if (socketWrapper.readOperation != null) {// 核心代码if (!socketWrapper.readOperation.process()) {closeSocket = true;}// 核心代码} else if (!processSocket(socketWrapper, SocketEvent.OPEN_READ, true)) {closeSocket = true;}}if (!closeSocket && sk.isWritable()) {if (socketWrapper.writeOperation != null) {if (!socketWrapper.writeOperation.process()) {closeSocket = true;}} else if (!processSocket(socketWrapper, SocketEvent.OPEN_WRITE, true)) {closeSocket = true;}}if (closeSocket) {cancelledKey(sk, socketWrapper);}}}} else {// Invalid keycancelledKey(sk, socketWrapper);}} catch (CancelledKeyException ckx) {cancelledKey(sk, socketWrapper);} catch (Throwable t) {ExceptionUtils.handleThrowable(t);log.error(sm.getString("endpoint.nio.keyProcessingError"), t);}
}

进入processSocket方法

public boolean processSocket(SocketWrapperBase<S> socketWrapper,SocketEvent event, boolean dispatch) {try {if (socketWrapper == null) {return false;}// 核心代码 线程SocketProcessorBase<S> sc = null;if (processorCache != null) {sc = processorCache.pop();}if (sc == null) {sc = createSocketProcessor(socketWrapper, event);} else {sc.reset(socketWrapper, event);}// 核心代码 获取线程池,通过线程池执行Executor executor = getExecutor();if (dispatch && executor != null) {executor.execute(sc);} else {sc.run();}} catch (RejectedExecutionException ree) {getLog().warn(sm.getString("endpoint.executor.fail", socketWrapper) , ree);return false;} catch (Throwable t) {ExceptionUtils.handleThrowable(t);// This means we got an OOM or similar creating a thread, or that// the pool and its queue are fullgetLog().error(sm.getString("endpoint.process.fail"), t);return false;}return true;
}

看看SocketProcessBase是什么?  实现了Runnable接口 

看下SocketProcessBase的run()方法

 @Overridepublic final void run() {synchronized (socketWrapper) {// It is possible that processing may be triggered for read and// write at the same time. The sync above makes sure that processing// does not occur in parallel. The test below ensures that if the// first event to be processed results in the socket being closed,// the subsequent events are not processed.if (socketWrapper.isClosed()) {return;}// 核心代码doRun();}}

看下doRun()方法

找到过滤器链ApplicationFilterChain,找到doFilter()方法

@Override
public void doFilter(ServletRequest request, ServletResponse response)throws IOException, ServletException {if( Globals.IS_SECURITY_ENABLED ) {final ServletRequest req = request;final ServletResponse res = response;try {java.security.AccessController.doPrivileged((java.security.PrivilegedExceptionAction<Void>) () -> {// 核心代码internalDoFilter(req,res);return null;});} catch( PrivilegedActionException pe) {Exception e = pe.getException();if (e instanceof ServletException)throw (ServletException) e;else if (e instanceof IOException)throw (IOException) e;else if (e instanceof RuntimeException)throw (RuntimeException) e;elsethrow new ServletException(e.getMessage(), e);}} else {internalDoFilter(request,response);}
}

定位到internalDoFilter()方法

private void internalDoFilter(ServletRequest request,ServletResponse response)throws IOException, ServletException {// Call the next filter if there is oneif (pos < n) {ApplicationFilterConfig filterConfig = filters[pos++];try {Filter filter = filterConfig.getFilter();if (request.isAsyncSupported() && "false".equalsIgnoreCase(filterConfig.getFilterDef().getAsyncSupported())) {request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR, Boolean.FALSE);}if( Globals.IS_SECURITY_ENABLED ) {final ServletRequest req = request;final ServletResponse res = response;Principal principal =((HttpServletRequest) req).getUserPrincipal();Object[] args = new Object[]{req, res, this};SecurityUtil.doAsPrivilege ("doFilter", filter, classType, args, principal);} else {filter.doFilter(request, response, this);}} catch (IOException | ServletException | RuntimeException e) {throw e;} catch (Throwable e) {e = ExceptionUtils.unwrapInvocationTargetException(e);ExceptionUtils.handleThrowable(e);throw new ServletException(sm.getString("filterChain.filter"), e);}return;}// We fell off the end of the chain -- call the servlet instancetry {if (ApplicationDispatcher.WRAP_SAME_OBJECT) {lastServicedRequest.set(request);lastServicedResponse.set(response);}if (request.isAsyncSupported() && !servletSupportsAsync) {request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR,Boolean.FALSE);}// Use potentially wrapped request from this pointif ((request instanceof HttpServletRequest) &&(response instanceof HttpServletResponse) &&Globals.IS_SECURITY_ENABLED ) {final ServletRequest req = request;final ServletResponse res = response;Principal principal =((HttpServletRequest) req).getUserPrincipal();Object[] args = new Object[]{req, res};SecurityUtil.doAsPrivilege("service",servlet,classTypeUsedInService,args,principal);} else {// 核心代码servlet.service(request, response);}} catch (IOException | ServletException | RuntimeException e) {throw e;} catch (Throwable e) {e = ExceptionUtils.unwrapInvocationTargetException(e);ExceptionUtils.handleThrowable(e);throw new ServletException(sm.getString("filterChain.servlet"), e);} finally {if (ApplicationDispatcher.WRAP_SAME_OBJECT) {lastServicedRequest.set(null);lastServicedResponse.set(null);}}
}

定位到service()方法的实现类HttpServlet中的service()方法中

@Overridepublic void service(ServletRequest req, ServletResponse res)throws ServletException, IOException {HttpServletRequest  request;HttpServletResponse response;try {request = (HttpServletRequest) req;response = (HttpServletResponse) res;} catch (ClassCastException e) {throw new ServletException(lStrings.getString("http.non_http"));}// 核心代码service(request, response);}

继续进入到service()方法中

protected final void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {long startTime = System.currentTimeMillis();Throwable failureCause = null;LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();LocaleContext localeContext = buildLocaleContext(request);RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());initContextHolders(request, localeContext, requestAttributes);try {// 核心代码doService(request, response);}catch (ServletException | IOException ex) {failureCause = ex;throw ex;}catch (Throwable ex) {failureCause = ex;throw new NestedServletException("Request processing failed", ex);}finally {resetContextHolders(request, previousLocaleContext, previousAttributes);if (requestAttributes != null) {requestAttributes.requestCompleted();}logResult(request, response, failureCause, asyncManager);publishRequestHandledEvent(request, response, startTime, failureCause);}
}

 进入doService()方法

 https://blog.csdn.net/z69183787/article/details/129240218
https://blog.csdn.net/sunshinezx8023/article/details/128630710

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/599556.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微信小程序如何搜索iBeacon设备

1.首先在utils文件夹下创建bluetooth.js和ibeacon.js 2.在 bluetooth.js文件中写入 module.exports {initBluetooth: function () {// 初始化蓝牙模块wx.openBluetoothAdapter({success: function (res) {console.log(蓝牙模块初始化成功);},fail: function (res) {console.l…

如何使用loki查询日志中大于某一数字的值的日志

简介 loki是一款轻量级的日志收集中间件&#xff0c;比elk体系占用的内存更小&#xff0c;采用go语言开发&#xff0c;可以利用grafana来查询loki中存储的日志&#xff0c;loki存储日志只对提前预设的标签做索引&#xff0c;所以日志存储空间占用比elk小很多。 方法 loki只对…

深圳易图讯实景三维数字孪生系统 实景三维电子沙盘

深圳易图讯实景三维数字孪生系统是一款基于三维地理空间的数字孪生系统&#xff0c;首先&#xff0c;该系统集成了多维度地理空间数据&#xff0c;可以将各类数据与应用需求进行充分整合&#xff0c;实现数据跨界融合、场景全角度可视等功能。其次&#xff0c;该系统具备智能化…

在 Windows 中安装 SQLite 数据库

在 Windows 上安装 SQLite 步骤1 请访问 SQLite 下载页面&#xff0c;从 Windows 区下载预编译的二进制文件 ​ 步骤2 您需要下载 sqlite-dll-win-x64-3440200.zip 和 sqlite-tools-win-x64-3440200.zip 压缩文件 步骤3 创建文件夹 C:\Program Files\SQLite&#xff0c;并在…

PostGIS学习教程十七:线性参考

PostGIS学习教程十七&#xff1a;线性参考 线性参考是一种表示要素的方法&#xff0c;这些要素可以通过引用一个基本的线性要素来描述。使用线性参照建模的常见示例包括&#xff1a; 公路资产&#xff0c;这些资产使用公路网络沿线的英里来表示。 道路养护作业&#xff0c;指…

java大数据hadoop2.92安装伪分布式文件系统

Apache Hadoop 3.3.6 – Hadoop: Setting up a Single Node Cluster. 1、解压缩到某个路径 /usr/local/hadoop 2、修改配置文件 /usr/local/hadoop/etc/hadoop/hadoop-env.sh export JAVA_HOME/usr/local/javajdk 3、修改配置文件 /usr/local/hadoop/etc/hadoop/core-sit…

(八)矩阵按键

文章目录 本章学习类比独立按键矩阵按键原理图三行代码法简单概述代码书写键码推算如何使用短按键长按键不松手长按键松手长按键 状态机法简单概述代码书写键码推算如何使用短按键长按键不松手长按键松手长按键 部分代码展示现象演示 本章学习类比独立按键 矩阵按键原理图 三行…

Swagger Editor 教程:从入门到精通编写 API 文档

在 API 开发的领域中&#xff0c;Swagger 以其卓越的使用效率与便捷性&#xff0c;备受开发者欢迎。它是一个强大的接口设计工具&#xff0c;允许开发人员对 RESTful API 进行高效的设计、构建及测试工作。本文旨在深入探讨其中一个子工具——Swagger Editor的使用介绍及它的有…

《知识扫盲》ROS和ROS2对比

文章摘选自&#xff1a;ROS与ROS2对比 1.ROS问题举例 ROS的设计目标是简化机器人的开发&#xff0c;如何简化呢&#xff1f;ROS为此设计了一整套通信机制&#xff08;话题、服务、参数、动作&#xff09;。 通过这些通信机制&#xff0c;ROS实现了将机器人的各个组件给的连接…

如何用自助法或刀切法来估计偏差、方差?

自助法和刀切法&#xff08;也叫水手刀法&#xff09;为计算标准误差和置信区间的非参数方法。刀切法耗费较少计算机资源&#xff0c;但自助法有某些统计优势。 1. 刀切法 由Quenouille(1949)提出的刀切法是用来对估计的偏差和方差进行近似的一个简单方法。 符号说明&#x…

VMware--安装CentOS系统

在虚拟机安装CentOS系统 1 下载CentOS镜像 方式一&#xff1a;可以到官网下载&#xff0c;下载速度较慢。 https://vault.centos.org/7.6.1810/isos/x86_64/ &#xff08;最后的 / 不要漏掉&#xff09; 方式二&#xff1a;可以到国内的镜像网站下载。 阿里开源镜像站&…

线性代数_逆矩阵性质

逆矩阵是线性代数中一个非常重要的概念&#xff0c;它具有以下几个基本性质&#xff1a; 1. 可逆矩阵一定是方阵&#xff1a;一个矩阵若要可逆&#xff0c;必须是方阵&#xff0c;即它的行数和列数相等。 2. 逆矩阵的唯一性&#xff1a;如果矩阵\( A \)是可逆的&#xff0c;那么…

sun.misc.BASE64Encoder() 找不到jar包

import sun.misc.BASE64Decoder;新下载的项目&#xff0c;在配置好maven之后&#xff0c;也更新完了Maven文件&#xff0c;还是发现有部分jar没有导入&#xff0c;报红信息如上所示。 其实这个是 Sun 的专用 API &#xff0c; rt.jar 是jre 中自带的 jar 包&#xff0c;所以就可…

ubuntu18.04+realsenseD455制作TUM数据集

教程目录 一、本机环境二、安装RealSense SDK三、录制rosbag四、制作数据集四、安装ROS-RealSense五、测试数据集一、本机环境 Ubuntu系统ROS系统RealSense18.04melodicD455二、安装RealSense SDK 1、首先注册服务器的公钥 sudo apt-key adv --keyserver keyserver.ubuntu.co…

全网最全fiddler使用教程和fiddler如何抓包(fiddler手机抓包)-笔者亲测

一、前言 抓包工具有很多&#xff0c;比如常用的抓包工具Httpwatch&#xff0c;通用的强大的抓包工具Wireshark.为什么使用fiddler?原因如下&#xff1a; 1.Wireshark是通用的抓包工具&#xff0c;但是比较庞大&#xff0c;对于只需要抓取http请求的应用来说&#xff0c;似乎…

Linux操作系统基础(09):Linux的文件权限

1. 文件权限是什么 在Linux系统中&#xff0c;文件权限是指对文件或目录的访问权限控制&#xff0c;它由三个部分组成&#xff1a;所有者权限、组权限和其他用户权限。文件权限和用户权限之间有密切的关系&#xff0c;文件权限规定了用户对文件的操作权限&#xff0c;而用户权…

CSIG青年科学家会议圆满举行,合合信息打造智能文档处理融合研究新范式

近期&#xff0c;第十九届中国图象图形学学会青年科学家会议&#xff08;简称“会议”&#xff09;在广州召开。会议由中国图象图形学学会&#xff08;CSIG&#xff09;主办&#xff0c;琶洲实验室、华南理工大学、中山大学、中国图象图形学学会青年工作委员会承办。会议面向国…

基于入侵杂草算法优化的Elman神经网络数据预测 - 附代码

基于入侵杂草算法优化的Elman神经网络数据预测 - 附代码 文章目录 基于入侵杂草算法优化的Elman神经网络数据预测 - 附代码1.Elman 神经网络结构2.Elman 神经用络学习过程3.电力负荷预测概述3.1 模型建立 4.基于入侵杂草优化的Elman网络5.测试结果6.参考文献7.Matlab代码 摘要&…

APP端网络测试与弱网模拟!

当前APP网络环境比较复杂&#xff0c;网络制式有2G、3G、4G网络&#xff0c;还有越来越多的公共Wi-Fi。不同的网络环境和网络制式的差异&#xff0c;都会对用户使用app造成一定影响。另外&#xff0c;当前app使用场景多变&#xff0c;如进地铁、上公交、进电梯等&#xff0c;使…

Ribbon客户端负载均衡

简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。 简单的说&#xff0c;Ribbon是Netflix发布的开源项目&#xff0c;主要功能是提供客户端的软件负载均衡算法和服务调用。Ribbon客户端组件提供一系列完善的配置项如连接超时&#xff0c;重试等…