Back to Javatutorial

SpringBoot - Ȳdevtools

docs/Spring全家桶/SpringBoot/SpringBoot自带的热部署工具.md

1.0.010.1 KB
Original Source

SpringBoot - Ȳdevtools

SpringBootУÿд޸ĶҪٵԣܱȽϷʱ䣻SpringBootŶԴṩspring-boot-devtoolsdevtoolsͼԵЧʡ@pdai

# ׼֪ʶ

# ʲôȲȼأ

ȲȼӦеʱԶ£¼ػ滻classȣӦõһPSspring-boot-devtoolsṩķҲҪģֶֻʵԶضѡ

ϸϣҪȲȼ, JavaĿԣ

  • Ȳ

    • ڷʱ²Ŀ
    • ֱ¼Ӧãַʽͷڴ棬ȼظӸɾףͬʱҲʱ䡣
  • ȼ

    • ʱ¼classӶӦá
    • ȼصʵԭҪjavaػʵַʽԸΪʱһ̨̣߳ʱļļʱ仯ʱˣ롣
    • ԱȷƣʱȡϢ̬ͨĵıΪ ȼʱͨ¼ظıϢֱӸıΪ

# ʲôLiveLoad

LiveLoadṩͻԶظµĹߣΪLiveLoadLiveload֣ devtoolsѾLiveLoadǿwebӦãԶˢ£ ʱԿLiveLoad.

ͬһʱֻһLiveReload ʼӦó֮ǰȷûLiveReloadСIDEӦóֻеһӦó֧LiveReload

# devtoolsʵȲ

ͨʵԶʽȲ

# POM

spring-boot-devtools

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        spring-boot-devtools
        <optional>true</optional> <!-- Էֹdevtoolsݵģ -->
    </dependency>
</dependencies>

# IDEA

ʹIDEAߣַͨʽ

  • ʽһ κʱֶ£Ctrl+F9

Ҳmvn compile봥£

  • ʽ IDEA迪ʱ룬Զ

1

File->Setting->Build,Execution,Deployment->Compile

ѡMake project automatically

2

ݼctrl+alt+shift+/

ѡRegistry

ѡcompiler.automake.allow.when.app.running

°汾IDEAFile->setting->Advanced Setttingsĵһã

# application.yml

spring:
  devtools:
    restart:
      enabled: true  #ÿȲ
      additional-paths: src/main/java #Ŀ¼
      exclude: WEB-INF/**
  thymeleaf:
    cache: false #ʹThymeleafģ棬رջ

# ʹLiveLoad

spring-boot-devtoolsģǶʽLiveReloadԴʱڴˢ¡ LiveReloadչ֧ChromeFirefoxSafariԴlivereload.comء

ߴأfirefox:

װ֮󣬿ͨͼ

㲻ӦóʱLiveReloadԽspring.devtools.livereload.enabledΪfalse

ͬһʱֻһLiveReload ʼӦó֮ǰȷûLiveReloadСIDEӦóֻеһӦó֧LiveReload

# һ

ȻһЩ߻ʹdevtoolߣǺܹģ¼⣬һ⡣@pdai

# devtoolԭΪλԶ

ΪʲôͬӦãΪʲôֶǽʹspring-boot-devtoolsȲ

spring-boot-devtoolsʹClassLoaderһClassLoaderزᷢĵࣨjarһClassLoaderrestart ClassLoaderػĵࣨԶࣩ

̨һļ̣߳File Watcher****Ŀ¼еļ䶯ʱ ԭrestart ClassLoader¼µrestart ClassLoader

Ϊļ䶯󣬵jar¼أֻԶ࣬صȽ٣ȽϿ졣

ҲΪʲôͬӦãΪʲôֶʹspring-boot-devtoolsȲ

ԶмҪע:

  • Զ¼־

¼ʲô־

ͨ¹ر

spring:
  devtools:
    restart:
      log-condition-evaluation-delta: false

  • ųһЩҪԶԴ

ijЩԴڸʱһҪĬ£ıԴ/META-INF/maven/META-INF/resources/resources/static/public/templatesȷᴥֳװҪԶЩųʹøspring.devtools.restart.excludeԡ磬Ҫų/static/public㽫ԣ

spring:
  devtools:
    restart:
      exclude: "static/**,public/**"

ҪЩĬֵųøspring.devtools.restart.additional-excludeԡ

  • Զ

ͨʹʵֵġڴӦóַЧܺáǣʱᵼ⡣

Ĭ£IDE еκδĿʹáأκγ.jarļʹáء㴦һģĿҲÿģ鶼뵽 IDE УҪԶһЩΪˣԴһMETA-INF/spring-devtools.propertiesļ

spring-devtools.propertiesļ԰restart.excludeΪǰ׺restart.includeincludeԪӦñߵĿԼexcludeҪӦ롰BaseĿԵֵӦ·ʽģʽʾʾ

restart:
  exclude:
    companycommonlibs: "/mycorp-common-[\\w\\d-\\.]+\\.jar"
  include:
    projectcommon: "/mycorp-myproj-[\\w\\d-\\.]+\\.jar"

صϢ´ڴ

# devtoolǷᱻJar

devtoolԭ˵ӦֻڿԵʱʹãjarʱDzҪģSpring᲻JAR

  • Ĭ£ᱻJAR

дӦóʱԱԶͨ java -jarʱᱻΪǡӦá

  • Զ̵Ӧ

ãֻεлʹ SSL бʱӦ

£devtoolҲ߱Զ̵ԵԶ̿ͻӦóּڴ IDE СҪorg.springframework.boot.devtools.RemoteSpringApplicationʹӵԶĿͬ·СӦóΨһӵԶ URL

磬ʹ Eclipse Spring Toolsһmy-appѲ Cloud Foundry ΪĿִ²

  1. ѡRun Configurations?Run˵
  2. һµJava Applicationá
  3. my-appĿ
  4. ʹorg.springframework.boot.devtools.RemoteSpringApplicationΪࡣ
  5. https://myapp.cfapps.ioProgram argumentsκԶ URL

еԶ̿ͻ˿б

  .   ____          _                                              __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _          ___               _      \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` |        | _ \___ _ __  ___| |_ ___ \ \ \ \
 \\/  ___)| |_)| | | | | || (_| []::::::[]   / -_) '  \/ _ \  _/ -_) ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |        |_|_\___|_|_|_\___/\__\___|/ / / /
 =========|_|==============|___/===================================/_/_/_/
 :: Spring Boot Remote :: 2.5.4

2015-06-10 18:25:06.632  INFO 14938 --- [           main] o.s.b.devtools.RemoteSpringApplication   : Starting RemoteSpringApplication on pwmbp with PID 14938 (/Users/pwebb/projects/spring-boot/code/spring-boot-project/spring-boot-devtools/target/classes started by pwebb in /Users/pwebb/projects/spring-boot/code)
2015-06-10 18:25:06.671  INFO 14938 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a17b7b6: startup date [Wed Jun 10 18:25:06 PDT 2015]; root of context hierarchy
2015-06-10 18:25:07.043  WARN 14938 --- [           main] o.s.b.d.r.c.RemoteClientConfiguration    : The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
2015-06-10 18:25:07.074  INFO 14938 --- [           main] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2015-06-10 18:25:07.130  INFO 14938 --- [           main] o.s.b.devtools.RemoteSpringApplication   : Started RemoteSpringApplication in 0.74 seconds (JVM running for 1.105)

# devtoolΪλĬϽûѡ

Spring Boot ֵ֧һЩʹû磬ģ滺ѱģԱظģļ⣬Spring MVC ṩ̬ԴʱӦ HTTP ͷ

Ȼ**зdz棬ڿпܻʵ䷴**ʹ޷ոӦóĸġԭ spring-boot-devtools ĬϽûѡ

Thymeleaf ṩspring.thymeleaf.cacheģĻ棬ʹspring-boot-devtoolsģʱDzҪֶЩԵģΪspring-boot-devtoolsԶá

ôԶЩأDevToolsPropertyDefaultsPostProcessorҵӦĬá

public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor {

	static {
		Map<String, Object> properties = new HashMap<>();
		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.servlet.session.persistent", "true");
		properties.put("spring.h2.console.enabled", "true");
		properties.put("spring.web.resources.cache.period", "0");
		properties.put("spring.web.resources.chain.cache", "false");
		properties.put("spring.template.provider.cache", "false");
		properties.put("spring.mvc.log-resolved-exception", "true");
		properties.put("server.error.include-binding-errors", "ALWAYS");
		properties.put("server.error.include-message", "ALWAYS");
		properties.put("server.error.include-stacktrace", "ALWAYS");
		properties.put("server.servlet.jsp.init-parameters.development", "true");
		properties.put("spring.reactor.debug", "true");
		PROPERTIES = Collections.unmodifiableMap(properties);
	}

Ȼ㲻뱻ӦԱspring-boot-devtoolsĬã ͨspring.devtools.add-propertiesfalseapplication.ymlС

# devtoolǷԸSpringbootӦȫֵã

ͨspring-boot-devtools.ymlļӵ$HOME/.config/spring-bootĿ¼**ȫ devtools **

ӵЩļκԶʹ devtools Spring Boot Ӧó磬ҪΪʼʹôļҪӵspring-boot-devtoolsļУ

spring:
  devtools:
    restart:
      trigger-file: ".reloadtrigger"

# Ҳdevtoolʲôѡ

Ҳdevtoolʲôѡ

ʵʵĿУҲȥʹdevtool, Ϊ

  • devtoolʽȻ滻JRebelǣշѵģ
  • ҪĻһȨ
    • ԶĿֶûʲô̫ôֶ
    • £ڲ޸Ļ߾̬Դ޸IDEAǿͨRebuildCtrl + Shift + F9ȸ

# ʾԴ

https://github.com/realpdai/tech-pdai-spring-demos

# ο

https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.devtools

https://liayun.blog.csdn.net/article/details/116541775


Ȩ@pdai ԭӣhttps://pdai.tech/md/spring/springboot/springboot-x-hello-devtool.html