Recently I stumbled upon a particular Spring project called Spring Loaded .The ReadMe section did catch my attention because it boosts developer productivity by reloading class files as and when they change.
There have been quite a few projects which provide such benefits and I believe the most popular commercial choice is JRebel. Those guys have done a great job in turning one of the greatest JAVA Web developers pain into a line of business. However there have been alternatives but none so simple as JRebel
Fortunately Spring Loaded was quite simple to use and configure. Its just a single jar. However, I am quite sure it is not a sophisticated as other commercial options. But its getting there and is currently being used in Grails 2.
ReLoading in Action
The most reliable way to show automatic reloading in action is by capturing a video. Unfortunately you guys will just have to trust me.
Start off by downloading the jar and including in your normal java project.
Create a POJO “Person”
public class Person { public void printBody() { System.out.println("*********"); this.hands(); // this.gloves(); System.out.println("Body"); this.legs(); // this.shoes(); System.out.println("*********"); } private void hands() { System.out.println("Hands"); } private void gloves() { System.out.println("Gloves"); } private void legs() { System.out.println("Legs"); } private void shoes() { System.out.println("Shoes"); } }
Now create a class to run the POJO
public class LiveEcho { public static void main(String[] args) throws Exception { while(true) { Thread.sleep(5000); new Person().printBody(); } } }
When you run the LiveEcho class you will see statements in the console like below.
*********
Hands
Body
Legs
*********
*********
Hands
Body
Legs
*********
*********
Hands
Body
Legs
*********
Nothing amazing. While the application is running, try uncommenting out “this.gloves()” or “this.shoes()”. Still nothing amazing.
In order to let Spring Loaded to be aware of the changes, we need to pass “-javaagent:springloaded-1.1.5.RELEASE.jar” as a java argument. I have done it here from my eclipse option
Run the LiveEcho class again. This time while the class is running, try uncommenting out “this.gloves()” or “this.shoes()”. You will see the output on the console changing
*********
Hands
Body
Legs
*********
*********
Hands
Body
Legs
*********
*********
Hands
Body
Legs
*********
*********
Hands
Gloves
Body
Legs
*********
*********
Hands
Gloves
Body
Legs
*********
*********
Hands
Gloves
Body
Legs
Shoes
*********
*********
Hands
Gloves
Body
Legs
Shoes
*********
As you can see, Spring Loaded has noticed the changes in the Person class files and reloaded the changes without re-starting the JVM.
Further reading
There are different ways of checking whether the classes have re-loaded or what are the changes that have taken place.
Q. How do I know when a reload has occurred so I can clear my state? A. You can write a plugin that is called when reloads occur and you can then take the appropriate action. Create an implementation of ReloadEventProcessorPlugin and then register it via SpringLoadedPreProcessor.registerGlobalPlugin(plugin). (There are other ways to register plugins, which will hopefully get some documentation!)
I have not tried it out on complex web project and their likes, but I am sure they are getting there.