今天试玩一下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。