日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【spring boot】支持webjars

發布時間:2024/9/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【spring boot】支持webjars 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

  • spring boot 2.4.3

spring boot 支持webjars

通過添加 ResourceHandlerRegistry 實現。有兩種方式可以實現:

  • spring boot默認對webjars提供支持。
  • 手動添加對webjars的支持。
  • 參考WebMvcConfigurationSupport。

    spring boot默認對webjars提供支持

    WebMvcAutoConfiguration類自動配置時,添加了對webjars的支持。

    @Overrideprotected 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));}});}

    WebMvcAutoConfiguration類自動配置的起效條件:

    @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 {... }

    當滿足WebMvcAutoConfiguration類自動配置的起效條件時,默認對webjars提供支持。當你引入的jar中,有付合webjars規則的,就可以通過webjars訪問到。比如:http://localhost:8080/webjars/jquery/3.1.1/jquery.min.js。

    當不滿足WebMvcAutoConfiguration類自動配置的起效條件時,不支持webjars。此時,需要手動添加對webjars的支持。

    手動添加對webjars的支持

    假如你按照某個教程自定義一個WebMvcConfigurationSupport,并得到了想要的功能時,WebMvcAutoConfiguration將不會起效。此時,如果還需要對webjars的支持,就需要手動添加對webjars的支持。(ps:盡量不要自定義WebMvcConfigurationSupport,讓WebMvcAutoConfiguration起效。WebMvcAutoConfiguration不起效后,那WebMvcAutoConfiguration中提供的默認配置就不起效。此種情況下,你需要手動添加那些本該默認提供的功能。另,大多數時候,你自定義WebMvcConfigurationSupport得到的功能,其實有另外一種方法。)

  • pom.xml
  • <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.3</version><relativePath /> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo-1</artifactId><version>0.0.1-SNAPSHOT</version><name>demo-1</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- spring-boot-devtools --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional> <!-- 表示依賴不會傳遞 --></dependency><dependency><groupId>org.webjars</groupId><artifactId>bootstrap</artifactId><version>3.3.7-1</version></dependency><dependency><groupId>org.webjars</groupId><artifactId>jquery</artifactId><version>3.1.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
  • 自定義WebMvcConfigurationSupport,添加 ResourceHandlerRegistry 實現。
  • @Configuration // 此時,WebMvcAutoConfiguration將不會起效 public class CustomWebMvcConfiguration extends WebMvcConfigurationSupport {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {if (!registry.hasMappingForPattern("/webjars/**")) {registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}} }
  • 啟動項目后,訪問地址:http://localhost:8080/webjars/jquery/3.1.1/jquery.min.js

    如果想要了解的更多,可以參考這里。
  • 參考

    https://spring.io/blog/2014/01/03/utilizing-webjars-in-spring-boot
    https://blog.coding.net/blog/spring-static-resource-process

    總結

    以上是生活随笔為你收集整理的【spring boot】支持webjars的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。