栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

WebMvcAutoConfiguration分析 版本 2.6.7

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WebMvcAutoConfiguration分析 版本 2.6.7

  1. 注解分析
    1.1 @Configuration(proxyBeanMethods = false) 指定@Bean方法是否应该被代理以强制bean的生命周期
    1.2 @ConditionalOnWebApplication(type = Type.SERVLET)Only servlet-based Web applications will match. 只有基于原生servlet应用程序时此类才会生效
    1.3 @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class }) 当这些类存在时此类才会生效
    1.4 @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)条件必须不满足任何要求才能匹配,并且要求不由同一bean满足。
    1.5 @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) 允许自动配置类排序 根据顺序值排序
    1.6 @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
    ValidationAutoConfiguration.class })在这些类配置之后,在进行此类配置。
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
   	ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
  1. WebMvcAutoConfiguration静态内部类 WebMvcAutoConfigurationAdapter分析
    2.1 点进去我们可以看到这个静态类上的一些注解和一个有参构造我们先看一下注解
    2.2@Import(EnableWebMvcConfiguration.class)引入开启mvc配置类然后点进去可以看到这个类上有一个@EnableConfigurationProperties(WebProperties.class)注解, 这个注解开启配置资源,也就是从配置文件加载需要的属性值 ,然后点进WebProperties 可以看到加载的那个配置文件,点进去之后我们可以看到@ConfigurationProperties(“spring.web”)注解,他对配置文件中以spring.web为前缀的资源加载进来
    2.3 @EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })
    @Order(0) 这个注解就是开启配置资源,就是对哪个类开启属性配置功能,然后我们分别点进他开启的这两个类可以看到,@ConfigurationProperties(prefix = “spring.mvc”)和@ConfigurationProperties(“spring.web”)这两个注解就是分别加载配置文件中以spring.mvc为前缀和以spring.web为前缀的配置
    2.4接下来看一下这个类中的有参构造WebMvcAutoConfigurationAdapter,
@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {
//WebProperties webProperties 这个参数包括了一些spring.web的配置
// WebMvcProperties mvcProperties 这个参数表示 mvc相关的一些配置属性
//ListableBeanFactory beanFactory bean工厂 这个接口中的方法将只考虑这个工厂的bean定义。它们会忽略任何通过其他方式注册的单例bean,比如org.springframework.beans.factory.config。ConfigurableBeanFactory的registerSingleton方法,除了getBeanNamesForType和getBeansOfType
//HttpMessageConverters 找到用于管理Spring Boot应用程序中使用的HttpMessageConverters的Bean
//ResourceHandlerRegistrationCustomizer 资源处理器的自定义器
//DispatcherServletPath 可由需要默认DispatcherServlet的路径详细信息的自动配置使用
//ServletRegistrationBean 用于在Servlet 3.0+容器中注册Servlet的ServletContextInitializer。类似于ServletContext提供的注册特性,但采用了Spring Bean友好的设计
public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties,
				ListableBeanFactory beanFactory, ObjectProvider messageConvertersProvider,
				ObjectProvider resourceHandlerRegistrationCustomizerProvider,
				ObjectProvider dispatcherServletPath,
				ObjectProvider> servletRegistrations) {
			this.resourceProperties = webProperties.getResources();
			this.mvcProperties = mvcProperties;
			this.beanFactory = beanFactory;
			this.messageConvertersProvider = messageConvertersProvider;
			this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
			this.dispatcherServletPath = dispatcherServletPath;
			this.servletRegistrations = servletRegistrations;
			this.mvcProperties.checkConfiguration();
		}

3.默认规则处理器

//ResourceHandlerRegistry  注册静态资源的处理器 可以设置缓存 ,在这里打个断点看一下这个方法
@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
		//判断默认静态资源是否开启 这个属性我们可以在配置文件中将他禁用 spring.web.resources.add-mappings=false这个属性默认为true 当为false时所有静态资源都不能被访问
			if (!this.resourceProperties.isAddMappings()) {
				logger.debug("Default resource handling disabled");
				return;
			}
			//添加资源处理器  所有的静态资源都会默认访问"/webjars/**", "classpath:/META-INF/resources/webjars/" 这两个路径
			addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
			addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
				registration.addResourceLocations(this.resourceProperties.getStaticLocations());
				if (this.servletContext != null) {
					ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
					registration.addResourceLocations(resource);
				}
			});
		}

4.欢迎页的处理规则

//应用程序欢迎页面的AbstractUrlHandlerMapping。支持静态文件和模板文件。如果静态和模板索引页面都可用,则首选静态页面
		@Bean
		public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
				FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
			WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
			//TemplateAvailabilityProvider bean的集合,可用于检查哪个(如果有的话)模板引擎支持给定的视图。缓存响应,除非spring.template.provider.cache属性设置为false
			//getWelcomePage(), 获取欢迎页 ,
					new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
					this.mvcProperties.getStaticPathPattern());
			//设置拦截器
			welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
			welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
			return welcomePageHandlerMapping;
		}

		private Resource getWelcomePage() {
		//private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/" }; 获取静态资源路径
			for (String location : this.resourceProperties.getStaticLocations()) {
				//获取路径中index.html页面资源描述符
				Resource indexHtml = getIndexHtml(location);
				if (indexHtml != null) {
					return indexHtml;
				}
			}
			ServletContext servletContext = getServletContext();
			if (servletContext != null) {
				return getIndexHtml(new ServletContextResource(servletContext, SERVLET_LOCATION));
			}
			return null;
		}
		private Resource getIndexHtml(String location) {
		//返回当前资源的描述
			return getIndexHtml(this.resourceLoader.getResource(location));
		}
		private Resource getIndexHtml(Resource location) {
			try {
			//创建此资源的资源处理
				Resource resource = location.createRelative("index.html");
				//判断是否以物理形式存在 及是否为空
				if (resource.exists() && (resource.getURL() != null)) {
					return resource;
				}
			}
			catch (Exception ex) {
			}
			return null;
		}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号