本文主要了解Spring Boot 1.3.0新添加的spring-boot-devtools模块的使用,该模块主要是为了提升开发者开发Spring Boot应用的用户体验。java
要想使用该模块须要在Maven中添加:git
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
或者在Gradle配置文件中添加:github
dependencies { compile("org.springframework.boot:spring-boot-devtools") }
在Spring Boot集成Thymeleaf时,spring.thymeleaf.cache
属性设置为false能够禁用模板引擎编译的缓存结果。如今,devtools会自动帮你作到这些,禁用全部模板的缓存,包括Thymeleaf, Freemarker, Groovy Templates, Velocity, Mustache等。更多的属性,请参考DevToolsPropertyDefaultsPostProcessor。web
你可能使用过 JRebel 或者 Spring Loaded来自动重启应用,如今只须要引入devtools就能够了,当代码变更时,它会自动进行重启应用。固然,你也可使用插件来实现,例如在maven中配置插件:正则表达式
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build>
bootRun { addResources = true }
DevTools在重启过程当中依赖于application context的shutdown hook,若是设置SpringApplication.setRegisterShutdownHook(false)
,则自动重启将不起做用。spring
静态资源文件在改变以后有时候不必触发应用程序重启,例如,Thymeleaf的模板会被变量替代。默认的,/META-INF/maven、/META-INF/resources、/resources、/static、/public或者/templates这些目录下的文件修改以后不会触发重启可是会触发LiveReload。能够经过spring.devtools.restart.exclude
属性来修改默认值浏览器
若是你想保留默认配置,并添加一些额外的路径,可使用spring.devtools.restart.additional-exclude
属性缓存
若是你想观察不在classpath中的路径的文件变化并触发重启,则能够配置 spring.devtools.restart.additional-paths 属性安全
设置 spring.devtools.restart.enabled 属性为false,能够关闭该特性。能够在application.properties中设置,也能够经过设置环境变量的方式app
public static void main(String[] args) { System.setProperty("spring.devtools.restart.enabled", "false"); SpringApplication.run(MyApp.class, args); }
经过设置spring.devtools.restart.trigger-file
属性指定一个文件,当该文件被修改时,则触发自动重启
Spring Boot自动重启使用的是两个类加载器,大多数状况下工做良好,有时候会出现问题。
默认的,IDE中打开的项目会使用 restart 类加载器进行加载,而任何其余的 .jar 文件会使用 base 类加载器进行加载。若是你使用的是多模块的项目,而且有些模块没有被导入到IDE,你须要建立并编辑META-INF/spring-devtools.properties
文件来自定义一些配置
spring-devtools.properties
文件包含有 restart.exclude
. 和 restart.include
. 前缀的属性。include属性文件的都会被加入到restart类加载器,exclude属性文件的都会被加入到base类加载器,他们的值是正则表达式,全部的属性值必须是惟一的
例如:
restart.include.companycommonlibs=/mycorp-common-[\\w-]+\.jar restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar
classpath中的全部META-INF/spring-devtools.properties文件都会被加载。
若是对象是使用ObjectInputStream进行反序列化,则自动重启将不可用。若是你须要反序列化对象,则你须要使用spring的ConfigurableObjectInputStream
并配合Thread.currentThread().getContextClassLoader()
进行反序列化
在浏览器方面,DevTools内置了一个LiveReload服务,能够自动刷新浏览器。若是你使用JRebel,则自动重启将会失效,取而代之的是使用动态加载类文件。固然,其余的DevTools(例如LiveReload和属性覆盖)特性还能使用。
该特性能够经过spring.devtools.livereload.enabled
属性来设置是否开启
能够在 $HOME 目录下建立一个.spring-boot-devtools.properties
文件来设置全局的配置。
例如,设置一个触发文件类触发重启:
spring.devtools.reload.trigger-file=.reloadtrigger
DevTools不只能够用于本地应用,也能够用于远程应用。经过设置 spring.devtools.remote.secret 属性能够开启远程应用。
远程DevTools支持包括两个部分,服务端应用和你IDE中运行的本地客户端应用。当设置spring.devtools.remote.secret属性以后,服务端应用自动开启DevTools特性,客户端程序须要手动启动。
远程客户端应用被设计来运行在你的IDE中。你须要使用和你链接的远程应用相同的classpath来运行org.springframework.boot.devtools.RemoteSpringApplication类,传递给该类的必选参数是你链接的应用的url。
例如,若是你在使用Eclipse或者STS,而且你有一个 my-app 应用部署在Cloud Foundry,你能够按照如下步骤操做:
一个运行中的远程客户端将会是这样子:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ ___ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | | _ \___ _ __ ___| |_ ___ \ \ \ \ \\/ ___)| |_)| | | | | || (_| []::::::[] / -_) ' \/ _ \ _/ -_) ) ) ) ) ' |____| .__|_| |_|_| |_\__, | |_|_\___|_|_|_\___/\__\___|/ / / / =========|_|==============|___/===================================/_/_/_/ :: Spring Boot Remote :: 1.3.2.RELEASE 2015-06-10 18:25:06.632 INFO 14938 --- [ main] o.s.b.devtools.RemoteSpringApplication : Starting RemoteSpringApplication on pwmbp with PID 14938 (/Users/pwebb/projects/spring-boot/code/spring-boot-devtools/target/classes started by pwebb in /Users/pwebb/projects/spring-boot/code/spring-boot-samples/spring-boot-sample-devtools) 2015-06-10 18:25:06.671 INFO 14938 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a17b7b6: startup date [Wed Jun 10 18:25:06 PDT 2015]; root of context hierarchy 2015-06-10 18:25:07.043 WARN 14938 --- [ main] o.s.b.d.r.c.RemoteClientConfiguration : The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'. 2015-06-10 18:25:07.074 INFO 14938 --- [ main] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2015-06-10 18:25:07.130 INFO 14938 --- [ main] o.s.b.devtools.RemoteSpringApplication : Started RemoteSpringApplication in 0.74 seconds (JVM running for 1.105)
说明:
spring.devtools.remote.proxy.host
和spring.devtools.remote.proxy.port
属性。远程客户端会监控你的应用的classpath的改变和本地重启的方式同样。任何更新的资源将会被推送到远程应用而且(若是有必要)触发一个重启。这在你集成一个使用云服务的本地不存在的特性时会是很是有用的。一般远程的更新和重启比一个完整的从新编译和部署周期会快的多。
只有在远程客户端运行的过程当中,文件才会被监控。若是你在启动远程客户端以前,修改一个文件,其将不会被推送到远程应用。
远程调试默认使用的端口是8000,你能够经过spring.devtools.remote.debug.local-port来修改。
你能够经过查看JAVA_OPTS来看远程调试是否被启用,主要是观察是否有-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n参数
QQ讨论群组:984370849 706564342 欢迎加入讨论
想要深刻学习的同窗们能够加入QQ群讨论,有全套资源分享,经验探讨,没错,咱们等着你,分享互相的故事!