SpringBoot----Web开发---静态资源处理-----首页处理,997页字节跳动Java面试真题解析火爆全网

@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,

	ValidationAutoConfiguration.class })

public class WebMvcAutoConfiguration {}




③看给容器中配置了什么



@SuppressWarnings("deprecation")

@Configuration(proxyBeanMethods = false)

@Import(EnableWebMvcConfiguration.class)

@EnableConfigurationProperties({ WebMvcProperties.class,

		org.springframework.boot.autoconfigure.web.ResourceProperties.class, WebProperties.class })

@Order(0)

public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer



配置文件的相关属性和xxx进行了绑定。WebMvcProperties—>spring.mvc、ResourceProperties—>spring.resources



**配置类只有一个有参构造器的情况你下,有参构造器的参数会自动装配**



*   ResourceProperties resourceProperties;获取和spring.resources绑定的所有的值的对象

*   WebMvcProperties mvcProperties 获取和spring.mvc绑定的所有的值的对象

*   ListableBeanFactory beanFactory Spring的beanFactory

*   HttpMessageConverters 找到所有HttpMessageConverters

*   ResourceHandlerRegistrationCustomizer 找到 资源处理器的自定义器。

*   DispatcherServletPath

*   ServletRegistrationBean 给应用注册Servlet、Filter…



public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties,

			ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,

			ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,

			ObjectProvider<DispatcherServletPath> dispatcherServletPath,

			ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {

		this.mvcProperties = mvcProperties;

		this.beanFactory = beanFactory;

		this.messageConvertersProvider = messageConvertersProvider;

		this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();

		this.dispatcherServletPath = dispatcherServletPath;

		this.servletRegistrations = servletRegistrations;

		this.mvcProperties.checkConfiguration();

	}



**资源处理的默认规则**



@Override

	protected void addResourceHandlers(ResourceHandlerRegistry registry) {

		super.addResourceHandlers(registry);

		if (!this.resourceProperties.isAddMappings()) {

			logger.debug("Default resource handling disabled");

			return;

		}

		ServletContext servletContext = getServletContext();

		addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");

		addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {

			registration.addResourceLocations(this.resourceProperties.getStaticLocations());

			if (servletContext != null) {

				registration.addResourceLocations(new ServletContextResource(servletContext, SERVLET_LOCATION));

			}

		});

	}

private void addResourceHandler(ResourceHandlerRegistry registry, String pattern,

			Consumer<ResourceHandlerRegistration> customizer) {

		if (registry.hasMappingForPattern(pattern)) {

			return;

		}

		ResourceHandlerRegistration registration = registry.addResourceHandler(pattern);

		customizer.accept(registration);

		registration.setCachePeriod(getSeconds(this.resourceProperties.getCache().getPeriod()));

		registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());

		customizeResourceHandlerRegistration(registration);

	}

spring:

mvc:

static-path-pattern: /res/**

resources:

add-mappings: false   禁用所有静态资源规则

@ConfigurationProperties(prefix = “spring.resources”, ignoreUnknownFields = false)

public class ResourceProperties {

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",

		"classpath:/resources/", "classpath:/static/", "classpath:/public/" };



/**

 * Locations of static resources. Defaults to classpath:[/META-INF/resources/,

 * /resources/, /static/, /public/].

 */

private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;



#### []( )欢迎页处理原理



	@Bean

	public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,

			FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {

		WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(

				new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),

				this.mvcProperties.getStaticPathPattern());

		welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));

		welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());

		return welcomePageHandlerMapping;

	}

WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,

		ApplicationContext applicationContext, Resource welcomePage, String staticPathPattern) {

	if (welcomePage != null && "/**".equals(staticPathPattern)) {

		logger.info("Adding welcome page: " + welcomePage);

		setRootViewName("forward:index.html");

	}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
img

1200页Java架构面试专题及答案

小编整理不易,对这份1200页Java架构面试专题及答案感兴趣劳烦帮忙转发/点赞

百度、字节、美团等大厂常见面试题

vip1024b (备注Java)**
[外链图片转存中…(img-3TlzKpYi-1711958812623)]

1200页Java架构面试专题及答案

小编整理不易,对这份1200页Java架构面试专题及答案感兴趣劳烦帮忙转发/点赞

[外链图片转存中…(img-8E25SW1l-1711958812624)]

[外链图片转存中…(img-UqrurhMs-1711958812624)]

百度、字节、美团等大厂常见面试题

[外链图片转存中…(img-3sYLrjzI-1711958812624)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值