Spring Boot怎么實(shí)現(xiàn)熱部署
在Spring Boot實(shí)現(xiàn)代碼熱部署是一件很簡(jiǎn)單的事情,代碼的修改可以自動(dòng)部署并重新熱啟動(dòng)項(xiàng)目。
1、引用devtools依賴(lài)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
這樣,當(dāng)修改一個(gè)java類(lèi)時(shí)就會(huì)熱更新。
2、自定義配置熱部署
以下配置用于自定義配置熱部署,可以不設(shè)置。
# 熱部署開(kāi)關(guān),false即不啟用熱部署 spring.devtools.restart.enabled: true # 指定熱部署的目錄 #spring.devtools.restart.additional-paths: src/main/java # 指定目錄不更新 spring.devtools.restart.exclude: test/**
3、Intellij Idea修改
如果是idea,需要改以下兩個(gè)地方:
1、勾上自動(dòng)編譯或者手動(dòng)重新編譯
File > Settings > Compiler-Build Project automatically
2、注冊(cè)
ctrl + shift + alt + / > Registry > 勾選Compiler autoMake allow when app running
注意事項(xiàng)
1、生產(chǎn)環(huán)境devtools將被禁用,如java -jar方式或者自定義的類(lèi)加載器等都會(huì)識(shí)別為生產(chǎn)環(huán)境。
2、打包應(yīng)用默認(rèn)不會(huì)包含devtools,除非你禁用SpringBoot Maven插件的excludeDevtools屬性。
3、Thymeleaf無(wú)需配置spring.thymeleaf.cache: false,devtools默認(rèn)會(huì)自動(dòng)設(shè)置,點(diǎn)擊參考完整屬性。
下面是devtools自動(dòng)配置的部分源碼:
@Order(Ordered.LOWEST_PRECEDENCE) public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor { private static final Map<String, Object> PROPERTIES; static { Map<String, Object> properties = new HashMap<String, Object>(); 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會(huì)在windows資源管理器占用java進(jìn)程,在開(kāi)發(fā)工具里面殺不掉,只能手動(dòng)kill掉,不然重啟會(huì)選成端口重復(fù)綁定報(bào)錯(cuò)。