Spring Boot实现热部署的实例方法
SpringBoot怎么实现热部署
在SpringBoot实现代码热部署是一件很简单的事情,代码的修改可以自动部署并重新热启动项目。
1、引用devtools依赖
org.springframework.boot spring-boot-devtools true
这样,当修改一个java类时就会热更新。
2、自定义配置热部署
以下配置用于自定义配置热部署,可以不设置。
#热部署开关,false即不启用热部署 spring.devtools.restart.enabled:true #指定热部署的目录 #spring.devtools.restart.additional-paths:src/main/java #指定目录不更新 spring.devtools.restart.exclude:test/**
3、IntellijIdea修改
如果是idea,需要改以下两个地方:
1、勾上自动编译或者手动重新编译
File>Settings>Compiler-BuildProjectautomatically
2、注册
ctrl+shift+alt+/>Registry>勾选CompilerautoMakeallowwhenapprunning
注意事项
1、生产环境devtools将被禁用,如java-jar方式或者自定义的类加载器等都会识别为生产环境。
2、打包应用默认不会包含devtools,除非你禁用SpringBootMaven插件的excludeDevtools属性。
3、Thymeleaf无需配置spring.thymeleaf.cache:false,devtools默认会自动设置,点击参考完整属性。
下面是devtools自动配置的部分源码:
@Order(Ordered.LOWEST_PRECEDENCE) publicclassDevToolsPropertyDefaultsPostProcessorimplementsEnvironmentPostProcessor{ privatestaticfinalMapPROPERTIES; static{ Map properties=newHashMap (); properties.put("spring.thymeleaf.cache","false"); properties.put("spring.freemarker.cache","false"); properties.put("spring.groovy.template.cache","false"); properties.put("spring.mustache.cache","false"); properties.put("server.session.persistent","true"); properties.put("spring.h2.console.enabled","true"); properties.put("spring.resources.cache-period","0"); properties.put("spring.resources.chain.cache","false"); properties.put("spring.template.provider.cache","false"); properties.put("spring.mvc.log-resolved-exception","true"); properties.put("server.jsp-servlet.init-parameters.development","true"); PROPERTIES=Collections.unmodifiableMap(properties); }
4、devtools会在windows资源管理器占用java进程,在开发工具里面杀不掉,只能手动kill掉,不然重启会选成端口重复绑定报错。
以上就是SpringBoot怎么实现热部署的详细内容,如果大家有任何补充可以联系毛票票小编。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。