日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot中的靜態(tài)資源訪問的實現(xiàn)

瀏覽:56日期:2023-04-25 15:59:02

一、說在前面的話

我們之間介紹過SpringBoot自動配置的原理,基本上是如下:

xxxxAutoConfiguration:幫我們給容器中自動配置組件;xxxxProperties:配置類來封裝配置文件的內(nèi)容;

二、靜態(tài)資源映射規(guī)則

1、對哪些目錄映射?

classpath:/META-INF/resources/ classpath:/resources/classpath:/static/ classpath:/public//:當(dāng)前項目的根路徑

2、什么意思?

就我們在上面五個目錄下放靜態(tài)資源(比如:a.js等),可以直接訪問(http://localhost:8080/a.js),類似于以前web項目的webapp下;放到其他目錄下無法被訪問。

3、為什么是那幾個目錄?

3.1、看源碼

我們一起來讀下源碼,這個是SpringBoot自動配置的WebMvcAutoConfiguration.java類來幫我們干的。

@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug('Default resource handling disabled'); return; } Integer cachePeriod = this.resourceProperties.getCachePeriod(); if (!registry.hasMappingForPattern('/webjars/**')) { customizeResourceHandlerRegistration(registry.addResourceHandler('/webjars/**') .addResourceLocations('classpath:/META-INF/resources/webjars/').setCachePeriod(cachePeriod)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern) .addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod)); }}

3.2、分析源碼

我們重點分析后半截,前半截后面會介紹。

// staticPathPattern是/**String staticPathPattern = this.mvcProperties.getStaticPathPattern();if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration( registry.addResourceHandler(staticPathPattern) .addResourceLocations( this.resourceProperties.getStaticLocations()) .setCachePeriod(cachePeriod));}this.resourceProperties.getStaticLocations()========>ResourcePropertiespublic String[] getStaticLocations() { return this.staticLocations;}========>private String[] staticLocations = RESOURCE_LOCATIONS;========>private static final String[] RESOURCE_LOCATIONS;private static final String[] SERVLET_RESOURCE_LOCATIONS = { '/' };private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { 'classpath:/META-INF/resources/', 'classpath:/resources/', 'classpath:/static/', 'classpath:/public/' };========>static { // 可以看到如下是對上面兩個數(shù)組進(jìn)行復(fù)制操作到一個新數(shù)組上,也就是合并。 RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length + SERVLET_RESOURCE_LOCATIONS.length]; System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0, SERVLET_RESOURCE_LOCATIONS.length); System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);}所以上述代碼經(jīng)過我的翻譯后成為了如下樣子:registry.addResourceHandler('/**').addResourceLocations( 'classpath:/META-INF/resources/', 'classpath:/resources/', 'classpath:/static/', 'classpath:/public/', '/') // 設(shè)置緩存時間 .setCachePeriod(cachePeriod));

3.3、一句話概括

WebMvcAutoConfiguration類自動為我們注冊了如下目錄為靜態(tài)資源目錄,也就是說直接可訪問到資源的目錄。

classpath:/META-INF/resources/ classpath:/resources/classpath:/static/ classpath:/public//:當(dāng)前項目的根路徑

優(yōu)先級從上到下。

所以,如果static里面有個index.html,public下面也有個index.html,則優(yōu)先會加載static下面的index.html,因為優(yōu)先級!

4、默認(rèn)首頁

PS:就是直接輸入ip:port/項目名稱默認(rèn)進(jìn)入的頁面。

4.1、看源碼

WebMvcAutoConfiguration.java@Beanpublic WelcomePageHandlerMapping welcomePageHandlerMapping( ResourceProperties resourceProperties) { return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(), this.mvcProperties.getStaticPathPattern());}

4.2、分析源碼

resourceProperties.getWelcomePage()========>public Resource getWelcomePage() { // 遍歷默認(rèn)靜態(tài)資源目錄后面拼接個index.html的數(shù)組 // 比如:[/static/index.html, /public/index.html等等] for (String location : getStaticWelcomePageLocations()) { Resource resource = this.resourceLoader.getResource(location); try { if (resource.exists()) {resource.getURL();return resource; } } catch (Exception ex) { // Ignore } } return null;}========>// 下面這段代碼通俗易懂,就是給默認(rèn)靜態(tài)資源目錄后面拼接個index.html并返回,比如:/static/index.htmlprivate String[] getStaticWelcomePageLocations() { String[] result = new String[this.staticLocations.length]; for (int i = 0; i < result.length; i++) { String location = this.staticLocations[i]; if (!location.endsWith('/')) { location = location + '/'; } result[i] = location + 'index.html'; } return result;}

所以上述代碼經(jīng)過我的翻譯后成為了如下樣子:

return new WelcomePageHandlerMapping( 'classpath:/META-INF/resources/index.html', 'classpath:/resources/index.html', 'classpath:/static/index.html', 'classpath:/public/index.html', '/index.html' , '/**');

4.3、一句話概括

WebMvcAutoConfiguration類自動為我們注冊了如下文件為默認(rèn)首頁。

classpath:/META-INF/resources/index.htmlclasspath:/resources/index.htmlclasspath:/static/index.html classpath:/public/index.html/index.html

優(yōu)先級從上到下。

所以,如果static里面有個index.html,public下面也有個index.html,則優(yōu)先會加載static下面的index.html,因為優(yōu)先級!

5、favicon.ico

PS:就是SpringBoot中的靜態(tài)資源訪問的實現(xiàn)這個圖標(biāo)。

5.1、看源碼

@Configuration@ConditionalOnProperty(value = 'spring.mvc.favicon.enabled', matchIfMissing = true)public static class FaviconConfiguration { private final ResourceProperties resourceProperties; public FaviconConfiguration(ResourceProperties resourceProperties) { this.resourceProperties = resourceProperties; } @Bean public SimpleUrlHandlerMapping faviconHandlerMapping() { SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(); mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1); mapping.setUrlMap(Collections.singletonMap('**/favicon.ico', faviconRequestHandler())); return mapping; } @Bean public ResourceHttpRequestHandler faviconRequestHandler() { ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler(); requestHandler .setLocations(this.resourceProperties.getFaviconLocations()); return requestHandler; }}

5.2、分析源碼

// 首先可以看到的是可以設(shè)置是否生效,通過參數(shù)spring.mvc.favicon.enabled來配置,若無此參數(shù),則默認(rèn)是生效的。@ConditionalOnProperty(value = 'spring.mvc.favicon.enabled', matchIfMissing = true)========》// 可以看到所有的**/favicon.ico都是在faviconRequestHandler()這個方法里找。mapping.setUrlMap(Collections.singletonMap('**/favicon.ico', faviconRequestHandler()));========》faviconRequestHandler().this.resourceProperties.getFaviconLocations()// 就是之前的五個靜態(tài)資源文件夾。 List<Resource> getFaviconLocations() { List<Resource> locations = new ArrayList<Resource>( this.staticLocations.length + 1); if (this.resourceLoader != null) { for (String location : this.staticLocations) { locations.add(this.resourceLoader.getResource(location)); } } locations.add(new ClassPathResource('/')); return Collections.unmodifiableList(locations);}

5.3、一句話概括

只要把favicon.ico放到如下目錄下,就會自動生效。

classpath:/META-INF/resources/ classpath:/resources/classpath:/static/ classpath:/public//:當(dāng)前項目的根路徑

6、webjars

6.1、看源碼

WebMvcAutoConfiguration@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug('Default resource handling disabled'); return; } Integer cachePeriod = this.resourceProperties.getCachePeriod(); if (!registry.hasMappingForPattern('/webjars/**')) { customizeResourceHandlerRegistration(registry.addResourceHandler('/webjars/**') .addResourceLocations('classpath:/META-INF/resources/webjars/').setCachePeriod(cachePeriod)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern) .addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod)); }}

6.2、分析源碼

這次我們來分析前半截。

Integer cachePeriod = this.resourceProperties.getCachePeriod();if (!registry.hasMappingForPattern('/webjars/**')) { customizeResourceHandlerRegistration( registry.addResourceHandler('/webjars/**') .addResourceLocations( 'classpath:/META-INF/resources/webjars/') .setCachePeriod(cachePeriod));}

6.3、一句話概括

所有/webjars/**都從classpath:/META-INF/resources/webjars/路徑下去找對應(yīng)的靜態(tài)資源。

6.4、什么是webjars?

就是以jar包的方式引入靜態(tài)資源。

官網(wǎng)地址:http://www.webjars.org/。類似于maven倉庫。

SpringBoot中的靜態(tài)資源訪問的實現(xiàn)

我們可以做個例子,將jquery引入到項目中

<dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.1</version></dependency>

看項目依賴

SpringBoot中的靜態(tài)資源訪問的實現(xiàn)

會自動為我們引入jquery,要怎么使用呢?我們上面說過:

所有/webjars/*都從classpath:/META-INF/resources/webjars/路徑下去找對應(yīng)的靜態(tài)資源。

所以我們啟動項目,訪問:http://localhost:8080/webjars/jquery/3.3.1/jquery.js即可。

必須在這幾個路徑下SpringBoot才會掃描到!

'classpath:/META-INF/resources/', 'classpath:/resources/','classpath:/static/', 'classpath:/public/' '/':當(dāng)前項目的根路徑

SpringBoot中的靜態(tài)資源訪問的實現(xiàn)

到此這篇關(guān)于SpringBoot中的靜態(tài)資源訪問的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot 靜態(tài)資源訪問內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
欧美91视频| 伊人精品久久| 亚洲欧洲日韩| 日本在线啊啊| 日韩高清成人在线| 一区二区自拍| 视频福利一区| 国产66精品| 国产激情久久| 亚州av乱码久久精品蜜桃| 国产高清日韩| 国产精品久久久久久妇女| 日本国产欧美| 亚洲精品极品| 免费在线成人网| 欧美一区=区| 在线国产一区二区| 久久久久午夜电影| 伊人久久在线| 日本а中文在线天堂| 精品少妇av| 伊人国产精品| 爽好多水快深点欧美视频| 妖精视频成人观看www| 日韩av福利| 日韩在线视频精品| 日韩在线观看| 99精品视频精品精品视频| 日韩在线精品| 欧美成人基地 | 精品久久免费| 欧美激情国产在线| 日韩理论片av| 99精品综合| 亚洲欧美伊人| 久久国产毛片| 在线一区免费| 热久久免费视频| 亚洲欧美久久精品| 欧美亚洲自偷自偷| 国产精品黄色片| 精品一级视频| 麻豆网站免费在线观看| 国产乱码午夜在线视频| 欧洲av不卡| 亚洲伦乱视频| 日韩高清中文字幕一区二区| 亚洲精品在线影院| 欧美不卡在线| 亚洲精品国产精品粉嫩| 日本欧美久久久久免费播放网| 日韩福利视频导航| 免费看一区二区三区| 久久精品国产亚洲aⅴ| 亚洲国产成人二区| 不卡中文字幕| 婷婷五月色综合香五月| 国产精品美女在线观看直播| 中文字幕成在线观看| 亚洲精品一区二区妖精| 亚洲精品韩国| 久久精品国产成人一区二区三区| 国产高清不卡| 亚洲中字黄色| 国产日韩欧美一区在线| 激情久久一区二区| 午夜精品久久久久久久久久蜜桃| 99精品视频在线| 亚洲精品裸体| 久久久久97| 亚洲欧美日韩高清在线| 日韩欧美中文字幕在线视频| 国产精品第一国产精品| 免费观看亚洲| 爽好多水快深点欧美视频| 国产精品久久久久久久久久齐齐 | 国产亚洲精品v| 久久精品99国产国产精| 日韩免费一区| 视频一区中文字幕国产| 欧美日本不卡高清| 国产欧美激情| 欧美天堂视频| 亚洲精品免费观看| 麻豆视频在线观看免费网站黄 | 免费一级欧美片在线观看网站| 樱桃视频成人在线观看| 美国三级日本三级久久99| 欧美黄色一区二区| 激情欧美丁香| 欧美日韩1区| 欧美日一区二区| 国产日韩在线观看视频| 蜜桃视频欧美| 久久99蜜桃| 蜜桃视频一区二区三区在线观看| 色爱综合网欧美| 色综合视频一区二区三区日韩| 成人在线黄色| 蜜臀av亚洲一区中文字幕| 日本一区二区高清不卡| 亚洲一区区二区| 国产亚洲精品美女久久 | 亚洲日本国产| 久久天堂av| 欧美片网站免费| re久久精品视频| 高清一区二区三区| 日韩不卡一二三区| 午夜日韩在线| 毛片在线网站| 国产欧美日韩影院| 免费不卡在线观看| 国产一区二区色噜噜| 欧美日韩一区二区三区四区在线观看 | 日韩免费在线| 国产欧美一区二区色老头| 亚洲欧洲一区| 日韩欧美一区二区三区在线观看| 中文字幕亚洲在线观看| 日韩精品诱惑一区?区三区| 欧美亚洲三区| 久久福利毛片| 999久久久亚洲| 精品美女视频| 日韩精品久久久久久久软件91| 午夜日本精品| av亚洲一区二区三区| 久久精品国产网站| 欧美三级第一页| 在线亚洲自拍| 欧美日韩第一| 国产精品99一区二区| 91偷拍一区二区三区精品| 国产精品一二| 奇米777国产一区国产二区| 亚洲欧美日韩视频二区| 亚洲一级二级| 国产99亚洲| 蜜桃精品在线| 色综合狠狠操| 久久精品国产久精国产| 美女精品在线| 在线亚洲成人| 丝袜美腿一区二区三区| 麻豆91精品| 男女激情视频一区| 综合色一区二区| 亚洲精品动态| 日韩av一区二区在线影视| 日韩精品成人在线观看| 欧美日韩va| 精品日韩一区| 伊人久久大香线蕉av不卡| 国产精品日韩欧美一区| 欧美精品一卡| 偷拍亚洲精品| 精品72久久久久中文字幕| 亚洲国内精品| 日韩中文字幕无砖| 精品午夜av| 性欧美69xoxoxoxo| 国产一区 二区| 精精国产xxxx视频在线野外| 石原莉奈在线亚洲二区| 国产精品啊啊啊| 亚洲福利免费| 91成人福利| 久久人人精品| 日韩高清三区| 国产在线看片免费视频在线观看| 91精品二区| 国产伦理久久久久久妇女| 999国产精品| 欧美精品三级在线| 久久精品亚洲人成影院| 亚洲精品国产精品粉嫩| 福利精品在线| 亚洲精品看片| 日韩欧美精品一区| 日韩精品免费一区二区夜夜嗨 | 一区二区91| 国产一区二区三区探花| 香蕉久久夜色精品国产| 国产一区2区在线观看| 模特精品在线| 麻豆视频久久| 伊人精品久久| 欧洲一区二区三区精品| 亚洲人成亚洲精品| 日韩精品第一区| 青青草国产精品亚洲专区无| 99久久亚洲精品| 久久的色偷偷| 日韩一区精品视频| 成人国产精品一区二区免费麻豆| 欧美在线综合| 999久久久国产精品| 国产精品免费99久久久| 久久亚洲视频|