Hot swapping is replacing or adding components without stopping or shutting down a system and it's very useful when you are working with Spring Framework, because it speeds up the development phase without forcing the developer to stop and restart his/her containers to apply code changes. This is typical scenario without hot swapping:

"Oh, I made a change, let's restart, I'll just have to wait a few seconds" thackle thackle... keyboard sounds.. "Uhm, I made a type, let's deploy it again..." thackle thackle... keyboard sounds.. "Ok, another change... why it's so frustrating..." thackle thackle... keyboard sounds..

Does it remebers you anything? :)

Stopping and restarting a web container can take from several seconds to several minutes based on the complexity of the project. So here it is how to achieve this goal with Spring Boot, Maven and Netbeans environment.

Maven setup

Spring Boot comes with devtools and spring-loaded, theirs features are:

  • Spring-Boot-devtools
    • Reload templates without restarting the container (reference)
    • Automatic restarts but in this case Spring-Loaded could be a better option.
    • An embedded LiveReload server that can be used to trigger a browser refresh when a resource is changed.(livereload.com)
  • Spring-Loaded
    • Reloads java classes without restarting the whole project (reference)

pom.xml

In order to enable devtools and spring-loaded, you need to add the following lines to your pom.xml

...
<dependencies>
    ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    ...
</dependencies>
...
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.6.RELEASE</version>
        </dependency>
    </dependencies>
</plugin>
...

Netbeans Setup

In order to enable hot swap you have to run the project in "Debug Mode". Every time you want to apply new changes to the running demo click on the "Apply Code Changes" button that appears on the toolbar near the debug button.

button

A faster way is to apply changes very time a Java file is saved. The option is available here:

Tools -> Options -> Java -> Java Debugger -> General -> Apply code changes after save

netbeans-screenshot

Hope that with this tips, you will improve your productivity on Netbeans.

Have a nice day! :)

Previous Post