Detecting if Spring Boot Devtools are active

849 views Asked by At

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?

2

There are 2 answers

1
Welsh On BEST ANSWER

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.

1
Elyorbek Ibrokhimov On

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.