-1

Spring Boot has a Devtools package which will turn on useful developer features such as autoreload in an IDE. It will disable itself in the fully packaged application.

Where are the technical details about how Spring Boot Devtools determines whether to become active, and more importantly, how can my application detect at runtime whether Devtools is active?

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272

2 Answers2

0

Adding Spring Boot DevTools to dependency list comes with auto-configured bean definitions such as DevToolsDataSourceAutoConfiguration, RemoteDevToolsAutoConfiguration and LocalDevToolsAutoConfiguration. Spring Boot picks those beans automatically as soon as it detects them in the classpath. live-reload, restart configurations come from LocalDevToolsAutoConfiguration class.

However, fully packaged applications don't include DevTools dependency when bundling executable archives using Spring Boot's Maven or Gradle plugin.

Developer tools are automatically disabled when running a fully packaged application. If your application is launched from java -jar or if it is started from a special classloader, then it is considered a “production application”. You can control this behavior by using the spring.devtools.restart.enabled system property. To enable devtools, irrespective of the classloader used to launch your application, set the -Dspring.devtools.restart.enabled=true system property. This must not be done in a production environment where running devtools is a security risk. To disable devtools, exclude the dependency or set the -Dspring.devtools.restart.enabled=false system property.

Reference Doc

Spring Boot Gradle Plugin and Spring Boot Maven Plugin documentation provide more specific details.

  • Yes, I read the paragraph you quoted. But that does not tell me _technical details_ about how Spring Boot Devtoops determines whether to become active. How does it tell whether my application is launched from `java -jar`? How does it check for special classloaders? Is there a method I can call to see if Devtools is active in the running context? Is there one or two lines of code that would do it? What are they? What should I be looking forin this `LocalDevToolsAutoConfiguration`? You gave me general details, which but I was looking for _technical details_ with some specifics. – Garret Wilson Mar 20 '23 at 14:25
0

I believe you are looking for the DevToolsEnablementDeducer which determines if dev tools is enabled within a specific thread based off the current stack and what is excluded by default.

Also, when built as a jar the spring-boot-maven-plugin will exclude the dependency if configured properly:

Devtools is automatically excluded by default (you can control that using the excludeDevtools property). In order to make that work with war packaging, the spring-boot-devtools dependency must be set as optional or with the provided scope.

Likewise, with gradle the recommended way of enabling also does not include when its packaged:

dependencies {
    developmentOnly("org.springframework.boot:spring-boot-devtools")
}

This means that if you choose to still include the dependency into your produced jar, then dev tools will then activate based off the existing LocalDevToolsAutoConfiguration which is loaded via the imports.

Welsh
  • 5,138
  • 3
  • 29
  • 43
  • `DevToolsEnablementDeducer.shouldEnable(Thread thread)` appears to be the sort of thing I was looking for. It would still be nice to know the specific steps DevTools takes to determine "[i]f your application is launched from java -jar or if it is started from a special classloader," but this is a good start. I'm working in another area of the code at the moment and haven't had time to test this, but I'll go ahead and assign the bounty. Thanks for digging this up. – Garret Wilson Mar 26 '23 at 21:06