In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you SpringBoot how to refresh static files in real time. I hope you will get something after reading this article. Let's discuss it together.
Scheme
Spring Boot has a WebMvcProperties configuration class in MVC and a ResourceProperties resource configuration class.
By default, Spring Boot looks for static resource files from the following directory
Private static final String [] CLASSPATH_RESOURCE_LOCATIONS = {"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}
The default static resource path match is / *.
Modify the Spring Boot configuration. Note that static-locations is the absolute path configuration of static resources in your computer.
Spirng resources: chain: cache: false static-locations: file:///Users/aihe/Desktop/taqu/ticket-system/src/main/resources/static/,file:///Users/aihe/Desktop/taqu/ticket-system/src/main/resources/ mvc: static-path-pattern: / static/**
When you modify the static file later, js,html,css and others can take effect immediately, and there is no need for hot-deployed plug-ins. It can be regarded as a little tip.
SpringBoot static resources do not restart refresh, custom configuration
Often do Spring Boot development, there are many times do not need front and rear separation, such as rear management system. During development, you need to restart every time you modify the page, which is extremely inconvenient. In fact, Spring Boot design does not need to be restarted, we just did not pay attention to the usage.
1. Demo
Pom & main
Org.springframework.boot spring-boot-starter-web 2.1.6.RELEASE @ SpringBootApplicationpublic class BootMain {public static void main (String [] args) {SpringApplication.run (BootMain.class, args);}}
Static resource file. Query the official docs document and put it in any directory of "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/" or "classpath:/public/" to load it.
Start main and access http://localhost:8082/index.html. The author has defined the port.
two。 How to take effect without restart
In fact, it is very simple. The directory in which Spring Boot takes effect is in the target directory, not our resource directory.
Spring Boot only recognizes target, how can our modified resources compile target automatically, so it's time to restart! ?
There are many solutions, including mvn compile or automatic compilation (not recommended, because resource files can also be compiled by java). In fact, there is a very simple one.
After each modification, right-click and recompile. I have already modified it. Let's try it.
It worked, huh?
3. Principle
Understanding the principle is helpful for us to customize Spring Boot and use it from the root.
As you can see, the code loaded by static resources is
Registry.addResourceHandler ("/ webjars/**") .addResourceLocations ("classpath:/META-INF/resources/webjars/")
Please automatically ignore the parameters in it, because we can customize it.
This is why many tutorials say that you can customize this method and not restart to refresh static resources, because the tutorials specify the path to the resource file, and the compiled path is the resource path.
Commonly used custom spring.mvc.staticPathPattern = "/ * *", default / * *, configure the path parameters of pattern,url
@ ConfigurationProperties (prefix = "spring.mvc") public class WebMvcProperties {private String staticPathPattern = "/ *"
Spring.resources.staticLocations = classpath:/html/, defaults to the following, which is the default configuration path of Spring Boot
@ 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; after reading this article, I believe you have some understanding of "how to refresh static files in real time by SpringBoot". If you want to know more about it, please follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.