spring-boot-devtools 模块包含一个内嵌的 LiveReload 服务器,它可以在资源改变时触发浏览器刷新。浏览器插件 LiveReload 可以免费从 livereload.com 站点获取,它支持 Chrome,Firefox,Safari 等浏览器。
你也可以到 Crx4Chrome 网站获取:https://www.crx4chrome.com/crx/1295/ ,或者到 Chrome 应用商店下载,如下图:
安装成功后,在浏览器右边会多出一个类似刷新的图标,如下图:
需要你的应用程序使用了 spring-boot-devtools 模块,Spring Boot 启动日志如下:
{spring.web.resources.chain.cache=false, spring.web.resources.cache.period=0} . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.0) 2020-11-20 22:06:22.968 INFO 12200 --- [ restartedMain] h.s.s.SpringbootDevtoolsDemo1Application : Starting SpringbootDevtoolsDemo1Application using Java 1.8.0_45 on MS-OYCYMLXUSLLD with PID 12200 (D:\learn\Spring\springboot\springboot_workspaces\springboot2\springboot_devtools\springboot_devtools_demo1\target\classes started by Administrator in D:\learn\Spring\springboot\springboot_workspaces\springboot2\springboot_devtools) 2020-11-20 22:06:22.969 INFO 12200 --- [ restartedMain] h.s.s.SpringbootDevtoolsDemo1Application : No active profile set, falling back to default profiles: default 2020-11-20 22:06:23.558 INFO 12200 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-11-20 22:06:23.560 INFO 12200 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-11-20 22:06:23.560 INFO 12200 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39] 2020-11-20 22:06:23.622 INFO 12200 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-11-20 22:06:23.622 INFO 12200 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 646 ms 2020-11-20 22:06:23.674 INFO 12200 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-11-20 22:06:23.702 INFO 12200 --- [ restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index 2020-11-20 22:06:23.829 INFO 12200 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2020-11-20 22:06:23.853 INFO 12200 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-11-20 22:06:23.862 INFO 12200 --- [ restartedMain] h.s.s.SpringbootDevtoolsDemo1Application : Started SpringbootDevtoolsDemo1Application in 0.961 seconds (JVM running for 179.479) 2020-11-20 22:06:23.864 INFO 12200 --- [ restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged 2020-11-20 22:06:23.924 INFO 12200 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2020-11-20 22:06:23.925 INFO 12200 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2020-11-20 22:06:23.926 INFO 12200 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
仔细查看上面的日志,其中“LiveReload server is running on port 35729”表示 LiveReload 服务运行在 35729 端口上面。此时,打开浏览器访问 Spring Boot。
我们把鼠标移动到刚刚安装的 LiveReload 插件上时,提示“Enable LiveReload”(插件图标中间的圆圈是空心的,如:)。点击一下该插件,插件内部的小圆圈变成实心(如:),且提示消息“LiveReload is connected. click to disable”
这时,试试修改 templates 下面的某个模板文件,然后保存。切换到浏览器,你会发现浏览器自动刷新了。
如果不想在运行应用时启动 LiveReload 服务器,你可以将 spring.devtools.livereload.enabled 属性设置为false。
注意:每次只能运行一个 LiveReload 服务器,如果你在 IDE中 启动多个应用,只有第一个能够获得动态加载功能。