Due to the abundance of features that Spring Framework provides, it is getting popular day by day. It was released in the year 2003. Spring Framework is basically a set or collection of sub-child frameworks. We need to write lots of code for manually setting the configuration of the Spring Application. To overcome this tedium and cumbersome effort, Spring Boot which is built on top of Spring Framework was released in 2014. The main moto of the Spring Boot framework is its ‘Auto-Configuration‘ feature. This enables a developer to build applications faster. One more crucial feature of Spring Boot is its DevTools library.
Pre-requisite: It is important to understand that the ‘DevTools’ is not an IDE plugin, nor does it require that you should use a specific IDE.
Spring Boot – DevTools
DevTools – As its name suggests, it provides the Spring developers with some development time tools which ease the development process. Some of these important workings are as follows –
1. Automatic application restart
- With DevTools when we make changes to Java code or properties file, the application gets updated with new changes.
- It monitors for changes and automatically restarts the application.
- With the DevTools in the application’s classpath, the application is loaded in two different class loaders in the Java Virtual Machine.
- One class loader contains all the dependencies ( Libraries ) that applications needs. These aren’t likely to change often.
- The second class loader contains your actual Java code, property files, etc particularly in ( src/main/ path ) which are likely to change frequently.
- On detecting a change, DevTools reloads only above the class loader with restarting the Spring application context and leaves another class loader, JVM intact.
- This will help reduce the amount of time to start the application.
- Devtools provides a way to configure global settings that are not coupled with any application. This file is named as .spring-boot-devtools.properties and is located at $HOME.
- If it is not configured manually, then defaults will be used by DevTools automatically.
Note: On automatic application restart, the changes to dependencies won’t be available. Because the respective class loader isn’t automatically reloaded. Therefore, after making the changes to dependencies, you have manually restart the application to get those changes in effect.
2. Automatic disable of template caches
- Spring Boot offers various template options like Thymeleaf, Groovy, FreeMarker, etc.
- These templates are configured to automatically cache the results of the template parsing.
- So, there is no need for them to be reparsed with each and every request that they serve.
- Cached templates make it difficult to make the changes to the templates while the application is running and see the desired changes after refreshing the browser.
- Even after applying the changes, the cached template will still be in use until you restart the application.
- This makes the Cached templates poor from the development perspective.
- DevTools helps us with this by automatically disabling all template caching.
- Due to this, your applied changes will now reflect on your templates after refreshing the browser.
Note:
- You can additionally turn on/off caching of templates by specifying ‘spring.thymeleaf.cache=true/false‘ in the ‘application.properties’ file.
- You can also override other properties as well in the same above file.
3. Automatic browser refresh
- If we have made some changes to the code and try to save it, then we have to click the refresh button on the browser each time to see its effect.
- This process automatically becomes time-consuming. It is always useful to witness the results in the browser and skip the refreshing of the browser again and again to reduce the development time.
- DevTools relieves us by skipping this cumbersome work for you.
- When we embed and use DevTools, it automatically enables a ‘LiveReload’ server along with your application.
- LiveReload server isn’t of much use, but it is useful when coupled with a corresponding LiveReload browser plugin.
- This makes your browser automatically refresh when the changes are made to the files that are served directly or indirectly by your browser. Example – Template engines, CSS stylesheets, JavaScript, and so on.
- LiveReload has browser plugins for browsers like Google Chrome, Firefox, and Safari.
- Internet Explorer and Edge browsers don’t have LiveReload browser plugins.
4. H2 Console
- Your application additionally may require a database.
- Spring Boot offers you a very essential feature of an embedded H2 database which is very helpful for development purposes.
- When we add H2 database dependency in the application build, DevTools will automatically enable an H2 console.
- You can access this console from your web browser itself.
- This helps us to get an insight or overview of the data that is used or handled by the application.
- To access this console, you have to simply point your web browser to – http://localhost:8080/h2-console.
To embed the Spring Boot DevTools in the application, add the following dependency in your project built –
Gradle
Gradle -> build.gradle dependencies { compile("org.springframework.boot:spring-boot-devtools") } OR dependencies { developmentOnly("org.springframework.boot:spring-boot-devtools") }
Example – build.gradle file
buildscript { repositories { jcenter() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.8.RELEASE") } } apply plugin: 'java' apply plugin: 'spring-boot' repositories { jcenter() } dependencies { compile("org.springframework.boot:spring-boot-starter-web") developmentOnly("org.springframework.boot:spring-boot-devtools") testCompile("org.springframework.boot:spring-boot-starter-test") }
Maven
Maven -> pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency>
Example – pom.xml file
XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >2.6.3</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >sia</ groupId > < artifactId >GFG</ artifactId > < version >0.0.1-SNAPSHOT</ version > < name >GFG</ name > < description >Lazyroar</ description > < properties > < java.version >11</ java.version > </ properties > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-thymeleaf</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > </ dependency > < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < optional >true</ optional > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-devtools</ artifactId > < scope >runtime</ scope > < optional >true</ optional > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < configuration > < excludes > < exclude > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > </ exclude > </ excludes > </ configuration > </ plugin > </ plugins > </ build > </ project > |
On adding DevTools dependencies, the word ‘devtools’ appears alongside your project name.
Example 1: Project files
Example 2: Boot dashboard
Note:
- As the DevTools is neither a plugin nor an IDE-specific functionality, it works equally well in the STS ( Spring Tool Suite ), IntelliJ IDEA, and also Netbeans.
- Also, we now know that the DevTools were built only for development-specific purposes, it is very smart enough that it disables itself when the application is deploying in a production setting.