Hot deploy java applications

Spring-loaded enables hot deployments for your applications which allows you to increase productivity by reducing developer cycle time.

Detailed Video Notes

Hey everyone, this is Justin at level up lunch. If you are getting started with spring boot or working with a traditional spring application you are probably looking for ways to reduce your cycle time. Spring has created a submodule called spring-loaded which allows you to hot deploy your application within your application server. Unlike traditional hot code replacements which allow simple changes on the JVM, spring-loaded provides you with the ability to add, modify or delete methods.

Project set up

[0:32]

By using a spring starter project, we created a spring-boot application. We are using spring sts for demonstration purposes but you should use an IDE of your choice. Next, we went ahead and created a sample controller with a method that returns a string to the default RequestMapping.

@RestController
public class MySampleController {

    @RequestMapping("/")
    public String home () {
        return "www.leveluplunch.com/";
    }
}

Download spring loaded

[1:8]

The next thing you will want to do is download spring loaded. Once you have it in a directory we will navigate to the run configuration in eclipse and in the VM arguments we will paste a string pointing to the jar. While pasting the location, don't forget the version of downloaded jar.

-javaagent:/Users/justinm/Documents/spring/spring-loaded/springloaded-1.1.5.RELEASE.jar -noverify

Spring loaded VM argument configuration

Now it is configured with spring loaded and it will give us the ability to perform hot deployments with our java application.

Demonstrating hot deployment

[1:49]

Once configured lets add another @RequestMapping to demonstrate the hot deployment in action. We create a snippet which contains a HotDeployment class and an associated @Requestmapping. When a file is saved, in our console we will see that it reloaded or refreshed. Once we navigate to localhost:8080/hotdeploy the changes were picked up and we don't need to stop and restart the application server.

@RequestMapping("/")
public String home () {
    return "www.leveluplunch.com/";
}

class HotDeploy {
    boolean isHotDeployable = false;

    public boolean isHotDeployable() {
        return isHotDeployable;
    }

    public void setHotDeployable(boolean isHotDeployable) {
        this.isHotDeployable = isHotDeployable;
    }
}

@RequestMapping(
        value = "/hotdeploy",
        method = RequestMethod.GET,
        produces = {MediaType.APPLICATION_JSON_VALUE}
        )
public HotDeploy hotDeployable () {

    HotDeploy hotDeploy = new HotDeploy();

    return hotDeploy;
}

The output when navigating to localhost:8080/hotdeploy:

Output

{"hotDeployable":false}

Adding a @RequestParam

[2:38]

We will add a @RequestParam to the /hotdeploy @RequestMapping named isHotDeployable. Based on that parameter we will set the value in the HotDeployable object. Once again, you will see in the console that application server refreshed.

@RequestMapping(
            value = "/hotdeploy",
            method = RequestMethod.GET,
            produces = {MediaType.APPLICATION_JSON_VALUE}
            )
public HotDeploy hotDeployable (@RequestParam(required=false) boolean isHotDeployable) {

    HotDeploy hotDeploy = new HotDeploy();

    if (isHotDeployable) {
        hotDeploy.setHotDeployable(isHotDeployable);
    }

    return hotDeploy;
}

The output when navigating to localhost:8080/hotdeploy?isHotDeployable=true:

Output

{"hotDeployable":true}

For the most part spring-loaded works pretty well but it does have limitations so be sure to check out the documentation.

Thanks for joining in today's screencast at level up lunch. Have a great day!