spring framework:
spring 模式注解
spring 应用上下文
spring工厂加载机制
spring 应用上下文初始化器
spring Environment 抽象
spring 应用事件/监听器

springboot:
springApplication
springApplication Builder APi
springApplication 运行监听器
springApplication 参数
springApplication 故障分析

RequestMappingHandlerMapping 的顶级接口 HandlerMapping (映射)
RequestMappingHandlerAdapter 的顶级接口 HandlerAdapter (适配)
HandlerExecutionChain (执行) 处理器的执行链
ViewResolver (视图解析)
HandlerExceptionResolver (异常处理)

HandlerAdapter 会返回一个ModelAndView ()

// HandlerAdapter 接口源码:
// 使用给定的处理程序处理此请求
@Nullable
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;

java springboot后端如何在请求没结束前不间断向前端传递数据_xml


蓝色是spring实现的

紫色是开发人员实现的

spring mvc 交互流程

①: 进入DispatcherServlet#doDispatch()方法

// 所有HTTP方法都由这个方法处理。这取决于handleradapter或处理程序他们自己决定哪些方法是可以接受的
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception{

}

②: 进入DispatcherServlet#getHandler() 方法

// 返回此请求的HandlerExecutionChain
@Nullable
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {

}

③:进入DispatcherServlet#getHandlerAdapter() 方法

// 返回此处理程序对象的HandlerAdapter
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {

}

④:

⑤:进入DispatcherServlet#resolveViewName() 方法

@Nullable
protected View resolveViewName(String viewName, @Nullable Map<String, Object> model,Locale locale,
 HttpServletRequest request) throws Exception {
 
}

⑥:

⑦:

⑧:

web mvc 注解驱动

注解配置 @Configuration( spring 范式注解)
组件激活 @EnableWebMvc(pring 模块装配)
自定义组件 webMvcConfigure(spring Bean)
模型属性 @ModelAttribut
请求头 @RequestHeader
Cookie @CookieValue
效验参数 @valid @ validated
注解处理 @ExceptionHandler
切面通知 @ControllerAdvice

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

}
// 使用 @EnableWebMvc 替代app-context.xml的配置
##  app-context.xml 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.imooc.web"/>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

使用@EnableWebMvc 后会自动将HandlerMapping和HandlerAdapter自动装配进来, 其实springframework(spring mvc时代)也可以做到springboot的自动装配

// @EnableWebMvc 注解引用了DelegatingWebMvcConfiguration类
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}

// DelegatingWebMvcConfiguration 类继承WebMvcConfigurationSupport 类
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { }

// WebMvcConfigurationSupport 类定义了 各种HandlerMapping 和HandlerAdapter;所以使用@EnableWebMvc注解会自动装配很多HandlerMapping 和HandlerAdapter (如下图)
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {}

// DispatcherServlet#initHandlerMappings() 初始化所有HandlerMapping  
// 初始化该类使用的HandlerMappings。
private void initHandlerMappings(ApplicationContext context) {
	this.handlerMappings = null;
	if (this.detectAllHandlerMappings) {
		// 在ApplicationContext中找到所有HandlerMappings,包括上下文  
		Map<String, HandlerMapping> matchingBeans =
				BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerMapping.class, true, false);
		if (!matchingBeans.isEmpty()) {
			this.handlerMappings = new ArrayList<>(matchingBeans.values());
			// We keep HandlerMappings in sorted order.
			AnnotationAwareOrderComparator.sort(this.handlerMappings);
		}
	}
	....
}

java springboot后端如何在请求没结束前不间断向前端传递数据_xml_02


java springboot后端如何在请求没结束前不间断向前端传递数据_mvc_03

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

//     <!--<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
//        <!--<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>-->
//        <!--<property name="prefix" value="/WEB-INF/jsp/"/>-->
//        <!--<property name="suffix" value=".jsp"/>-->
//    <!--</bean>-->
	// 使用类配置替代 上面xml的配置
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                System.out.println("拦截中...");
                return true;
            }
        });
    }
}

web mvc 自动装配

servlet 依赖: Servlet 3.0+
servlet SPI : ServletContainerInitializer (容器启动的时候可以做回调)
spring适配: SpringServletContainerInitializer

web mvc 自动装配依赖 Servlet 3.0中一个自动转配的接口ServletContainerInitializer;spring适配的ServletContainerInitializer的类是SpringServletContainerInitializer

Spring SPI: WebApplicationlnitializer
编程驱动: AbstractDispatcherServletInitializer
注解驱动: AbstractAnnotationConfigDispatcherServletInitializer

springboot时代的简化

完全自动装配

  • DispatcherServlet : DispatcherServletAutoConfiguration
  • 替换@EnableWebMvc : WebMvcAutoConfiguration
  • Servlet 容器 :ServletWebServerFactoryAutoConfiguration

装配条件

web类型:servlet
API依赖:Servlet psring Web MVC
bean依赖:webMvcConfiguartionSupport

外部化配置

web MVC 配置: WebMvcProperties
资源配置: ResourceProperies

小技巧

查找Properties或者yaml配置 (在idea中)可以通过 ctrl+shift+f 通过scope类型 选择范围查找

All Places 所有地方
Project Files 项目文件
Project and Libraries 项目和库
Project Production Files 项目生产文件
Project Test Files 项目测试文件
Scratches and Consoles 划痕和游戏机
Recently Viewed Files 最近查看的文件
Recently Changed Files 最近修改的文件
Open Files 打开的文件
Current File 当前文件