The goal

It is the ultimate goal of most programmers to become more efficient and build software quicker. One way to improve is to learn to type faster, which obviously helps but will not get you far. A probably better approach would be to expose yourself to harder problems so that you gain experience and next time you stumble upon the same problem you already have an idea how to solve it.

However, in the world of compiled languages there is one common hurdle we all need to overcome, and that is the long feedback loop (fix, save, build, redeploy, wait, check result, repeat).

A low-hanging fruit

If you are building your Java backend applications using Spring Boot then there is a quick and easy step you can take to immediately make this feedback loop faster.

Instead of manually restarting your application every time you do a change:

…you can just add this dependency and get automatic app reload for free:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <optional>true</optional>
  <!--
    make sure it is marked as optional
    (otherwise it will be bundled up with your app)
  -->
</dependency>
dependencies {
  developmentOnly("org.springframework.boot:spring-boot-devtools")
}

Now, when you change your code and rebuild the project the application will automatically restart.

What’s cool is that this automatic restart should be much faster because it reloads only your own classes while keeping other non-project classes intact.

It is achieved by maintaining 2 separate classloaders:

  • base classloader which loads classes outside your project (like all those classes from the dependency jars) - this classloader is preserved between restarts
  • second classloader holds your project classes and is thrown away whenever restart occurs
Spring Boot is highly customizable and so are Developer Tools. You can learn more from the official documentation.

Live-reload in the browser

Another time saver is the live-reload feature which refreshes the page after the server restarts. In layman’s terms, you no longer need to hit the refresh button in your browser.

All you need is install a live-reload plugin, like this one for Chrome:

After you enable the live-reload feature on your site:

…you can enjoy having the page refreshed for you:

Squeeze out even more time

If you are a performance-fixated maniac constantly obsessed with improving your daily workflow then sooner or later you will start getting annoyed by having to wait several seconds for the restart to complete. This wait time increases as the project grows larger and could get out of hand.

Fortunately, there are more options you can consider. For example, you can look into the realm of JVM class reloading. Basically, this means using tools that are replacing bytecode in the JVM while it is still running.

Reloading classes for free

There is a cost free solution to reload your application in almost an instant. It works surprisingly well and is called DCEVM, which stands for Dynamic Code Evolution Virtual machine.

In order to use it you just replace your current JDK with an alternative JDK. It all boils down to downloading a certain version of DCEVM JDK (as of time of writing this blog post Java 8 and 11 are supported), unpacking it to a folder and then configuring it in your IDE:

Once you have that you just need to create hotswap-agent.properties within src/main/resources, then fill it with the following content:

# hotswap-agent.properties
autoHotswap=true

…and Bob’s your uncle. Now you have live class reload:

If you are using Spring Boot in version 2.1.0 and above then you can easily confirm that the configuration was successful.

You should observe the following statement in your logs (here is the corresponding pull request for reference) about the restart being suspended in favour of DCEVM reloads:

[main] INFO org.springframework.boot.devtools.restart.RestartApplicationListener - Restart disabled due to an agent-based reloader being active

Is it any good?

Although DCEVM does not cost a dime it works pretty well and ships with a lot of plugins that help with reloading changes related to a particular Java library.

Here you can check the list of available plugins - it basically covers most of the major Java frameworks. There is even a DCEVM plugin dedicated for Spring framework which automatically reloads Spring's application context.

Not so free alternatives

Even though DCEVM is great, configuring it for a legacy project can be quite challenging. If you have to make it work with a brown-field application then you should accept the likelihood that you are going to get stuck.

There is, however, an alternative and it is called JRebel. It is hassle-free and usually works out-of-the-box. The “only” downside is that it is quite pricey so you have to weigh up the pros and cons. If you can afford it (or stretch your project’s budget) then it is definitely worth considering.

Please check this post about JRebel if you would like to learn more.