JRebel Tutorial: Using JRebel Spring Integration

JRebel Spring plugin takes advantage of JRebel class reloading to reload Spring dependencies. It supports registering new Spring beans, adding/removing dependencies and adding new MVC controllers and handler methods. These can be done using either XML configuration or annotations. The minimal supported Spring version is 2.0.x.

To use the JRebel Spring plugin it helps to understand that it will work in two stages:

  1. Reload all changes to configuration. This stage will reload all Spring XMLs and find new @Component/@Service/@Controller annotated beans. It will not have any effect on existing beans, but all beans created after that will be configured according to the newly loaded metadata.
  2. Call AutowireCapableBeanFactory.configureBean() on all singletons that had code changes in this session. This will reinject all configured field or method dependencies on those singletons.
Such two-staged process is important since reloading configuration changes is cheap while reinjecting bean dependencies may create or reinitialize beans and is expensive.

Example: Adding a new dependency via XML

You have a configuration file applicationContext.xml and a configured bean myBean for class MyBean. You add a new setter method

public void setMyProperty(String property) {
  System.out.println(property);
}
to MyBean and add <property name="myProperty" value="Success!"/> to the myBean configuration. After you save and access your application the following lines will be printed in the console:
JRebel: Reloading class 'MyClass'
JRebel-Spring: Reloading Spring bean definitions in 'applicationContext.xml'.
JRebel-Spring: Reconfiguring bean 'myBean' [MyClass]
Success!

Example: Adding a new dependency via annotations

You have a configured singleton bean MyBean with the @Service annotation. You create a new class AnotherBean annotated with @Component:

@Component
public class AnotherBean {
  public void success() {
    System.out.println("Success!");
  }
}
You also add the following setter method to MyBean class:
@Autowired
public void setAnotherBean(AnotherBean ab) {
  ab.success();
}
After you save and access your application the following lines will be printed in the console:
JRebel: Reloading class 'MyClass'
JRebel-Spring: Reconfiguring bean 'MyBean' [MyBean]
Success!

Example: Adding a controller and handler via annotations

You have a configured Spring MVC web application. You create a new controller class MyController with the handler method myHandler():

@Controller
public class MyController {
  @RequestMapping("/hello.do")
  public void myHandler() {
  }
}
You also create a hello.jsp with the following content:
<html>
<body>
Hello, Spring!
</body>
</html>
After you save and access /hello.do in the browser you can see “Hello, Spring!” message.

Now you can also add a second handler method myHandler2() and bind it to /hello2.do. You also need to create a hello2.jsp with the message “Hello, JRebel!”. After you save and access /hello2.do in the browser you will see the message “Hello, JRebel!” as well as the following lines printed in the console:

JRebel: Reloading class 'MyController'
JRebel-Spring: Reconfiguring bean 'MyController' [MyController]