spring-boot-devtools
Developer tools are automatically disabled when running a fully packaged application. If your application is launched using java -jar or if it’s started using a special classloader, then it is considered a “production application”. Flagging the dependency as optional in Maven or using compileOnly in Gradle is a best practice that prevents devtools from being transitively applied to other modules using your project
// maven
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
// gradle
dependencies {
compileOnly("org.springframework.boot:spring-boot-devtools")
}
- 开发阶段我们会更倾向于实时看到改动产生的效果,因此关闭一些默认的配置是有必要的,比如缓存,我们可以手动application.properties文件配置,比如spring.thymeleaf.cache=false,更直接的,devtools已经为开发环境准备好了配置:
// DevToolsPropertyDefaultsPostProcessor.class ...
static {
HashMap devToolsProperties = new HashMap();
devToolsProperties.put("spring.thymeleaf.cache", "false");
devToolsProperties.put("spring.freemarker.cache", "false");
devToolsProperties.put("spring.groovy.template.cache", "false");
devToolsProperties.put("spring.mustache.cache", "false");
devToolsProperties.put("server.session.persistent", "true");
devToolsProperties.put("spring.h2.console.enabled", "true");
devToolsProperties.put("spring.resources.cache-period", "0");
devToolsProperties.put("spring.resources.chain.cache", "false");
devToolsProperties.put("spring.template.provider.cache", "false");
devToolsProperties.put("spring.mvc.log-resolved-exception", "true");
devToolsProperties.put("server.jsp-servlet.init-parameters.development", "true");
devToolsProperties.put("spring.reactor.stacktrace-mode.enabled", "true");
PROPERTIES = Collections.unmodifiableMap(devToolsProperties);
}
- 对于位于/static, /templates目录下的静态资源,并不需要restart,而是更轻便的live reload,默认情况下/META-INF/ maven, /META-INF/resources, /resources, /static, /public or /templates目录下的改动不会导致restart,当然你可以配置不包含哪些目录或者添加额外的资源目录spring.devtools.restart.exclude=static/,public/ spring.devtools.restart.additional-exclude