zoukankan      html  css  js  c++  java
  • spring tomcat启动 请求处理

    onRefresh();
    protected void onRefresh() { 
            try {
                createEmbeddedServletContainer();
            } 
        }
    private void createEmbeddedServletContainer() {
            EmbeddedServletContainer localContainer = this.embeddedServletContainer;   //null
            ServletContext localServletContext = getServletContext();  //null
            if (localContainer == null && localServletContext == null) {
                EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();   //返回org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory 
                this.embeddedServletContainer = containerFactory
                        .getEmbeddedServletContainer(getSelfInitializer());  //this = AnnotationConfigEmbeddedWebApplicationContext,返回
            } 
            initPropertySources();
        }
    protected EmbeddedServletContainerFactory getEmbeddedServletContainerFactory() { 
            String[] beanNames = getBeanFactory()
                    .getBeanNamesForType(EmbeddedServletContainerFactory.class);   //[tomcatEmbeddedServletContainerFactory]
            return getBeanFactory().getBean(beanNames[0],EmbeddedServletContainerFactory.class);
        }
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
            return doGetBean(name, requiredType, null, false);//tomcatEmbeddedServletContainerFactory,interface org.springframework.boot.context.embedded.EmbeddedServletContainerFactory
        } 
    protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {  
            try { 
                Object bean = resolveBeforeInstantiation(beanName, mbdToUse); 
            } 
    
            Object beanInstance = doCreateBean(beanName, mbdToUse, args); 
            return beanInstance;
        }
    
    protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) { 
            if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) { 
                if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {  
                        bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName); 
                } 
            }
            return bean;
        }
    protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
            for (BeanPostProcessor bp : getBeanPostProcessors()) {
                if (bp instanceof InstantiationAwareBeanPostProcessor) {
                    InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                    Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);  //AnnotationAwareAspectJAutoProxyCreator判断要不要生成代理, 
                }
            }
            return null;
        }
    protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) {  
            if (instanceWrapper == null) {
                instanceWrapper = createBeanInstance(beanName, mbd, args);  //org.springframework.beans.BeanWrapperImpl: wrapping object [org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory@7a7cc52c]  
            Object exposedObject = bean; 
                populateBean(beanName, mbd, instanceWrapper);
                if (exposedObject != null) {
                    exposedObject = initializeBean(beanName, exposedObject, mbd);
                } 
            return exposedObject;
        }
    protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {  
            Object wrappedBean = bean; 
                wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); 
                wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); 
            return wrappedBean;
        }
    public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) { 
            Object result = existingBean;
            for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
                result = beanProcessor.postProcessAfterInitialization(result, beanName);
                if (result == null) {
                    return result;
                }
            }
            return result;
        }
    TomcatEmbeddedServletContainerFactory 类:
    原来是通过server.xml解析的,这里是代码写死的。
    public EmbeddedServletContainer getEmbeddedServletContainer(
                ServletContextInitializer... initializers) {
            Tomcat tomcat = new Tomcat();//org.apache.catalina.startup.Tomcat@620c8641
            File baseDir = (this.baseDirectory != null ? this.baseDirectory
                    : createTempDir("tomcat"));//C:UsersADMINI~1AppDataLocalTemp	omcat.5396461944750847035.8086
            tomcat.setBaseDir(baseDir.getAbsolutePath());
            Connector connector = new Connector(this.protocol);
            tomcat.getService().addConnector(connector);
            customizeConnector(connector);
            tomcat.setConnector(connector);
            tomcat.getHost().setAutoDeploy(false);
            configureEngine(tomcat.getEngine());
            for (Connector additionalConnector : this.additionalTomcatConnectors) {
                tomcat.getService().addConnector(additionalConnector);
            }
            prepareContext(tomcat.getHost(), initializers);
            return getTomcatEmbeddedServletContainer(tomcat);
        }
     public Connector(String protocol) {
            setProtocol(protocol); 
            ProtocolHandler p = null;
            try {
                Class<?> clazz = Class.forName("org.apache.coyote.http11.Http11NioProtocol");
                p = (ProtocolHandler) clazz.getConstructor().newInstance();
            } finally {
                this.protocolHandler = p;
            } 
        }
    public Server getServer() {
            if (server != null) {
                return server;
            }
            System.setProperty("catalina.useNaming", "false");
            server = new StandardServer();
            initBaseDir();
            server.setPort( -1 );
            Service service = new StandardService();
            service.setName("Tomcat");
            server.addService(service);
            return server;
        }
    public Host getHost() {
            Engine engine = getEngine();
            if (engine.findChildren().length > 0) {
                return (Host) engine.findChildren()[0];
            }
            Host host = new StandardHost();
            host.setName(hostname);
            getEngine().addChild(host);
            return host;
        }
    protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
            return new TomcatEmbeddedServletContainer(tomcat, getPort() >= 0);
        }
    public TomcatEmbeddedServletContainer(Tomcat tomcat, boolean autoStart) {
            Assert.notNull(tomcat, "Tomcat Server must not be null");
            this.tomcat = tomcat;
            this.autoStart = autoStart;
            initialize();
        }
    private void initialize() throws EmbeddedServletContainerException { 
            synchronized (this.monitor) {
                try {
                    addInstanceIdToEngineName();
                    try {
                        final Context context = findContext(); 
                        this.tomcat.start(); 
                }
            }
        }
    public void start() throws LifecycleException {
            getServer();
            getConnector();
            server.start();
        }
    public final synchronized void start() throws LifecycleException {
            if (state.equals(LifecycleState.NEW)) {
                init();
            } 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);
                startInternal();
                if (state.equals(LifecycleState.FAILED)) { 
                    stop();
                } else if (!state.equals(LifecycleState.STARTING)) { 
                    invalidTransition(Lifecycle.AFTER_START_EVENT);
                } else {
                    setStateInternal(LifecycleState.STARTED, null, false);
                }
            }  
        }
    public final synchronized void init() throws LifecycleException { 
            try { 
                initInternal(); 
            }  
        }
    protected void initInternal() throws LifecycleException {
            for (int i = 0; i < services.length; i++) {
                services[i].init();
            }
        }
    public final synchronized void init() throws LifecycleException { 
            try { 
                initInternal(); 
            }  
        }
    protected void initInternal() throws LifecycleException { 
            if (engine != null) {
                engine.init();
            } 
            for (Executor executor : findExecutors()) {
                if (executor instanceof JmxEnabled) {
                    ((JmxEnabled) executor).setDomain(getDomain());
                }
                executor.init();
            } 
            mapperListener.init();
            synchronized (connectorsLock) {
                for (Connector connector : connectors) {
                    try {
                        connector.init();   //里面会adapter = new CoyoteAdapter(this);,
                    }  
                }
            }
        }
    解析requestMappingHandlerMapping是时候, 
    public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)  {
            Object result = existingBean;//requestMappingHandlerMapping
            for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
                result = beanProcessor.postProcessBeforeInitialization(result, beanName);//beanProcessor = ApplicationContextAwareProcessor, 
            }
            return result;
        }
    ApplicationContextAwareProcessor类:
    public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { 
            else {
                invokeAwareInterfaces(bean);
            }  
            return bean;
        }
    protected void initInterceptors() {
            if (!this.interceptors.isEmpty()) {
                for (int i = 0; i < this.interceptors.size(); i++) {
                    Object interceptor = this.interceptors.get(i); //[WebRequestHandlerInterceptorAdapter, ConversionServiceExposingInterceptor, ResourceUrlProviderExposingInterceptor]
                    this.adaptedInterceptors.add(adaptInterceptor(interceptor));
                }
            }
        }
    initializeBean方法里面的invokeInitMethods(beanName, wrappedBean, mbd);  把url跟controller的映射注册到requestMappingHandlerMapping进去了
    protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) {
                else {
                    ((InitializingBean) bean).afterPropertiesSet();
                }
            }
            if (mbd != null) {
                String initMethodName = mbd.getInitMethodName();
                if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                        !mbd.isExternallyManagedInitMethod(initMethodName)) {
                    invokeCustomInitMethod(beanName, bean, mbd);
                }
            }
        } 
    requestMappingHandlerMapping类:
    public void afterPropertiesSet() {   
            super.afterPropertiesSet();
        }
    protected void initHandlerMethods() { 
            String[] beanNames =  getApplicationContext().getBeanNamesForType(Object.class));   //所有的bean
            for (String beanName : beanNames) {
                if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
                    Class<?> beanType = null;
                    try {
                        beanType = getApplicationContext().getType(beanName);
                    } 
                    if (beanType != null && isHandler(beanType)) {
                        detectHandlerMethods(beanName);   //beanName = accountController
                    }
                }
            }
            handlerMethodsInitialized(getHandlerMethods());
        }
    protected boolean isHandler(Class<?> beanType) {
            return (AnnotatedElementUtils.hasAnnotation(beanType, Controller.class) ||
                    AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
        }
    protected void detectHandlerMethods(final Object handler) {
            Class<?> handlerType = (handler instanceof String ?
                    getApplicationContext().getType((String) handler) : handler.getClass());   //class com.hcxy.car.controller.AccountController
            final Class<?> userType = ClassUtils.getUserClass(handlerType);   //class com.hcxy.car.controller.AccountController
            Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
                    new MethodIntrospector.MetadataLookup<T>() {
                        @Override
                        public T inspect(Method method) {
                            try {
                                return getMappingForMethod(method, userType);
                            } 
                        }
                    });//{public Map  AccountController.addAccount(java.util.Map)  n={[/ota/account/add],methods=[POST]}, public  Map  AccountController.all1( )={[/ota/account/all],methods=[GET]}}
     
            for (Map.Entry<Method, T> entry : methods.entrySet()) {
                Method invocableMethod = AopUtils.selectInvocableMethod(entry.getKey(), userType);
                T mapping = entry.getValue();
                registerHandlerMethod(handler, invocableMethod, mapping);
            }
        }
    请求进来。从tomcat走到DispatcherServlet。
    
    DispatcherServlet类:
    
    protected void onRefresh(ApplicationContext context) {
            initStrategies(context);
        }
    protected void initStrategies(ApplicationContext context) {
            initMultipartResolver(context);
            initLocaleResolver(context);
            initThemeResolver(context);
            initHandlerMappings(context);
            initHandlerAdapters(context);
            initHandlerExceptionResolvers(context);
            initRequestToViewNameTranslator(context);//DefaultRequestToViewNameTranslator加到DispatcherServlet
            initViewResolvers(context);//DefaultRequestToViewNameTranslator加到DispatcherServlet
            initFlashMapManager(context);//DefaultRequestToViewNameTranslator加到DispatcherServlet
        }
    private void initHandlerMappings(ApplicationContext context) {
            this.handlerMappings = null;   //this = DispatcherServlet
            if (this.detectAllHandlerMappings) {
                Map<String, HandlerMapping> matchingBeans =
                        BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);   //从容器找到所有的HandlerMapping,{requestMappingHandlerMapping=RequestMappingHandlerMapping@5ad6f98e, viewControllerHandlerMapping=WebMvcConfigurationSupport$EmptyHandlerMapping@786ff0ea, beanNameHandlerMapping=BeanNameUrlHandlerMapping@15186ce0, resourceHandlerMapping=SimpleUrlHandlerMapping@54bb1194, defaultServletHandlerMapping=WebMvcConfigurationSupport$EmptyHandlerMapping@1109730f, faviconHandlerMapping=SimpleUrlHandlerMapping@25109608, welcomePageHandlerMapping=WebMvcAutoConfiguration$WelcomePageHandlerMapping@38dbeb39}
                if (!matchingBeans.isEmpty()) {
                    this.handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values());   //7个,
                    AnnotationAwareOrderComparator.sort(this.handlerMappings);
                }
            }
        }
    private void initHandlerAdapters(ApplicationContext context) {
            this.handlerAdapters = null;  //this = DispatcherServlet
            if (this.detectAllHandlerAdapters) {
                Map<String, HandlerAdapter> matchingBeans =
                        BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerAdapter.class, true, false);   //从容器中找{requestMappingHandlerAdapter=RequestMappingHandlerAdapter@45297e7, httpRequestHandlerAdapter=HttpRequestHandlerAdapter@5423a17, simpleControllerHandlerAdapter=SimpleControllerHandlerAdapter@42ff9a77}
                if (!matchingBeans.isEmpty()) {
                    this.handlerAdapters = new ArrayList<HandlerAdapter>(matchingBeans.values());
                    AnnotationAwareOrderComparator.sort(this.handlerAdapters);
                }
            }
        }
    private void initHandlerExceptionResolvers(ApplicationContext context) {
            this.handlerExceptionResolvers = null;    //this = DispatcherServlet
            if (this.detectAllHandlerExceptionResolvers) {
                Map<String, HandlerExceptionResolver> matchingBeans = BeanFactoryUtils
                        .beansOfTypeIncludingAncestors(context, HandlerExceptionResolver.class, true, false);  //{errorAttributes=DefaultErrorAttributes@30cafd13, handlerExceptionResolver=HandlerExceptionResolverComposite@60f662bd}
                if (!matchingBeans.isEmpty()) {
                    this.handlerExceptionResolvers = new ArrayList<HandlerExceptionResolver>(matchingBeans.values());
                    AnnotationAwareOrderComparator.sort(this.handlerExceptionResolvers);
                }
            }
    private void initViewResolvers(ApplicationContext context) {
            this.viewResolvers = null;//this = DispatcherServlet
            if (this.detectAllViewResolvers) {
                // Find all ViewResolvers in the ApplicationContext, including ancestor contexts.
                Map<String, ViewResolver> matchingBeans =
                        BeanFactoryUtils.beansOfTypeIncludingAncestors(context, ViewResolver.class, true, false);
                if (!matchingBeans.isEmpty()) {
                    this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.values());
                    //[org.springframework.web.servlet.view.BeanNameViewResolver@29090809, org.springframework.web.servlet.view.ViewResolverComposite@22aee519, org.springframework.web.servlet.view.InternalResourceViewResolver@7ba06506, org.springframework.web.servlet.view.ContentNegotiatingViewResolver@3e2c4d4b, org.thymeleaf.spring4.view.ThymeleafViewResolver@780129bc]
                    AnnotationAwareOrderComparator.sort(this.viewResolvers);
                }
            }
    private void initFlashMapManager(ApplicationContext context) {
            try {//this = DispatcherServlet
                this.flashMapManager = context.getBean(FLASH_MAP_MANAGER_BEAN_NAME, FlashMapManager.class); //SessionFlashMapManager
            }
    请求进来,然后进到doService方法。
    protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
            try {
                doDispatch(request, response);
            } 
        }
    protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception { 
    HandlerExecutionChain mappedHandler = null;    //拦截器链
            try { 
                try {
                    processedRequest = checkMultipart(request);   //上床文件,
                    multipartRequestParsed = (processedRequest != request); 
                    mappedHandler = getHandler(processedRequest); //public java.util.Map  com.hcxy.car.controller.AccountController.all1()
                    HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());  // ha = RequestMappingHandlerAdapter 
                    if (!mappedHandler.applyPreHandle(processedRequest, response)) {  //前置拦截器
                        return;
                    }
        //反射调用controller的方法。
                    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
        
        applyDefaultViewName(processedRequest, mv);
                    mappedHandler.applyPostHandle(processedRequest, response, mv);   //后置拦截器
                } 
            } 
        }
    protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
            for (HandlerMapping hm : this.handlerMappings) {//[SimpleUrlHandlerMapping@7afbf2a0, RequestMappingHandlerMapping@349f0ca4, BeanNameUrlHandlerMapping@52ca0ad4, SimpleUrlHandlerMapping@2565a7d0, WebMvcConfigurationSupport$EmptyHandlerMapping@47f2c722, WebMvcConfigurationSupport$EmptyHandlerMapping@4fd7b79, WebMvcAutoConfiguration$WelcomePageHandlerMapping@7e050be1],只有RequestMappingHandlerMapping有url和controller的对应关系。
                HandlerExecutionChain handler = hm.getHandler(request);  //hm= RequestMappingHandlerMapping
                if (handler != null) {
                    return handler;//public java.util.Map com.hcxy.car.controller.AccountController.all1()
                }
            }
            return null;
        }
    protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
            for (HandlerAdapter ha : this.handlerAdapters) { //[RequestMappingHandlerAdapter@6e2eead5, HttpRequestHandlerAdapter@113eed88, SimpleControllerHandlerAdapter@5f0677f3]
                if (ha.supports(handler)) {
                    return ha;//RequestMappingHandlerAdapter
                }
            } 
        }
    boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
            HandlerInterceptor[] interceptors = getInterceptors();//拦截器
            if (!ObjectUtils.isEmpty(interceptors)) {
                for (int i = 0; i < interceptors.length; i++) {
                    HandlerInterceptor interceptor = interceptors[i];
                    if (!interceptor.preHandle(request, response, this.handler)) { 
                    }
                    this.interceptorIndex = i;
                }
            }
            return true;
        }
    反射调用controller的方法。
    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());   //ha = RequestMappingHandlerAdapter
    protected ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception { 
            else {
                mav = invokeHandlerMethod(request, response, handlerMethod);
            } 
            return mav;
        }
    protected <T> void writeWithMessageConverters(T value, MethodParameter returnType,
                ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)  {
            if (selectedMediaType != null) {
                selectedMediaType = selectedMediaType.removeQualityValue();
                for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
                    if (messageConverter instanceof GenericHttpMessageConverter) {
                        if (((GenericHttpMessageConverter) messageConverter).canWrite(
                                declaredType, valueType, selectedMediaType)) {
                            outputValue = (T) getAdvice().beforeBodyWrite(outputValue, returnType, selectedMediaType,
                                    (Class<? extends HttpMessageConverter<?>>) messageConverter.getClass(),
                                    inputMessage, outputMessage);   //{@11105} accounts -> {@11106} 12345,返回的Map。
                            if (outputValue != null) {
                                addContentDispositionHeader(inputMessage, outputMessage);
                                ((GenericHttpMessageConverter) messageConverter).write(
                                        outputValue, declaredType, selectedMediaType, outputMessage); 
                            }
                            return;
                        }
                    } 
                }
            } 
        }
    objectWriter.writeValue(generator, value);   //写出去数据给流浪器,从response写出去。
    processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);     //视图响应
    
    if (mv != null && !mv.wasCleared()) {//
                render(mv, request, response);   //渲染
                if (errorView) {
                    WebUtils.clearErrorRequestAttributes(request);
                }
            }
    protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception { 
    
            View view;
            if (mv.isReference()) { 
                view = resolveViewName(mv.getViewName(), mv.getModelInternal(), locale, request); 
                }
            }
            else { 
                view = mv.getView(); 
            }
     
            try {
                if (mv.getStatus() != null) {
                    response.setStatus(mv.getStatus().value());
                }
                view.render(mv.getModelInternal(), request, response);
            } 
        }
    protected View resolveViewName(String viewName, Map<String, Object> model, Locale locale,
                HttpServletRequest request) throws Exception {
    
            for (ViewResolver viewResolver : this.viewResolvers) {   //viewResolvers是视图解析器
                View view = viewResolver.resolveViewName(viewName, locale);
                if (view != null) {
                    return view;
                }
            }
            return null;
        }
    public View resolveViewName(String viewName, Locale locale) throws Exception {
            if (!isCache()) {
                return createView(viewName, locale);
            }
  • 相关阅读:
    “智慧城市”长啥样?人工智能打造最具幸福感生态城
    崔宝秋:技术是小米立业之本
    搜狗发布新研究:语音+唇语让语音识别更准确
    当深圳变成一座数字花园
    中科大开发出“超长焦相机”,最远能监控 45 公里内的目标
    斯坦福黑科技打造新型交互机器人:看视频一学就会!
    机器人已经开始影响你的生活 你准备好了吗?
    说取代医生为时尚早,但AI已为颠覆医疗业埋下伏笔
    Exception testing
    Matchers and assertthat
  • 原文地址:https://www.cnblogs.com/yaowen/p/11881385.html
Copyright © 2011-2022 走看看