今天試玩一下Spring-boot,瞬間就愛上了它,可是發現并沒有像傳說中的在開發模式下可以不用重啟就看到更新的結果。經過一番搜索,找到了Spring-boot-devtools,看名字就知道,應該是我想要的,趕緊配置。
compile("org.springframework.boot:spring-boot-devtools:$spring_boot_version")
懷著激動的心情,run起來,改代碼,刷新頁面。。。。結果超出了我的預期,沒有任何變化,還是需要重啟服務,甚至只是改html模版也需要重啟。
不死心,繼續搜索,最終找到了答案,原來是因為Intellij IEDA和Eclipse不同,Eclipse設置了自動編譯之后,修改類它會自動編譯,而IDEA在非RUN或DEBUG情況下才會自動編譯(前提是你已經設置了Auto-Compile)
設置其實很簡單:
首先,IDEA設置里面這里必須打勾
image.png
然后 Shift+Ctrl+Alt+/,選擇Registry(Mac 的快捷鍵是:Command+Shift+option+/)
image.png
進去之后,找到compiler.automake.allow.when.app.running,打勾
image.png
一切搞定,重啟下項目,然后改動代碼后刷新就可以看到結果了。
另,百度找到的大多數spring-boot都是用maven構建的,我比較喜歡gradle,所以在這貼上build.gradle的代碼以作記錄。
group 'com.cisetech.warm'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.4-3'
ext.spring_boot_version = "1.5.7.RELEASE"
repositories {
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'spring-boot'
sourceCompatibility = 1.8
repositories {
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
jcenter()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: "$spring_boot_version"
compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: "$spring_boot_version"
compile("org.springframework.boot:spring-boot-devtools:$spring_boot_version")
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: "$spring_boot_version"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
bootRun{
addResources = true
}
最后,在找解決方案的時候,看到國外有大神是和Docker一起用,能夠像作Vue項目一樣,改動代碼后自動瀏覽器上就有變化了,這個還是沒懂怎么起效果的,有知道的同學歡迎mark。