Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.

Using Spring MVC with JRebel: Adding and autowiring beans without restarting

Spring is one of the most popular Java web frameworks in existence, it provides amazing benefit in providing a reliable, tested framework for developing web applications in Java. Developers don’t have to reinvent the wheel when it comes to well understood problems like serving content over HTTP and wiring up a model-view-controller (MVC) layout for their application.

Spring’s core functionality is to provide an Inversion of Control (IoC) platform for Dependency Injection (DI). There are many benefits of loosely coupling components through DI, however application initialization takes longer than if everything were tightly coupled in code without a DI platform. Creating and modifying Spring beans is a normal, everyday activity for Java developers utilizing Spring in their applications.

In Spring bean relationships can be wired using the @Autowired annotation like this:

public class MovieRecommender {
    private MovieCatalog movieCatalog;
    private CustomerPreferenceDao customerPreferenceDao;

The annotation lets Spring manage and resolve the relationships between the various beans in your environment. The previous example just injected a field into the MovieRecommender class, however the @Autowired annotation is also capable of wiring constructors and methods like this:

public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
    this.customerPreferenceDao = customerPreferenceDao;

Spring is well-used and liked because it enables developers to write less original code to accomplish normal, everyday tasks. But, it does have a long initialization sequence which means that every redeploy of your application or restart of your application server will take even longer. The more complicated your application’s dependency graph the longer the initialization time wasted.

No fear though, JRebel works great with Spring and over 80 other popular Java frameworks. JRebel enables developers to make almost any Java code change without rebuilding, redeploying, or restarting. JRebel replaces code in the running JVM, so developers can view code changes instantly.

Let’s say we want to add a new bean to the Spring petclinic application. We’ll define the new bean in the petclinic-servlet.xml file, add the new MyNameBean class to our project, then autowire the bean into our AddOwnerForm controller.

First, let’s define the bean in our xml file:

<bean id="namebean" class="org.springframework.samples.petclinic.MyNameBean">
    <property name="name" value="ZeroTurnaround" /> 

Then, we add the POJO MyNameBean class to our project:

package org.springframework.samples.petclinic;

public class MyNameBean {
    private String name;

    public String getName() {
        return name;

    public void setName(String name) { = name;


Finally, we wire our bean with the @Autowired annotation:

@SessionAttributes(types = {Owner.class})
public class AddOwnerForm {

    private final Clinic clinic;

    MyNameBean mybean;
    public AddOwnerForm(Clinic clinic) { = clinic;


    @RequestMapping(method = RequestMethod.GET)
    public String setupForm(Model model) {
        Owner owner = new Owner();
        return "owners/form";

Before the change, our form loads and it’s completely blank:

After making the change, we see JRebel reloaded the affected classes and reloaded the spring bean definitions in the xml file as well:

2014-08-29 15:26:12 JRebel: Reloading Spring bean definitions in 'file:/Users/koblentz/demo_materials/workspaces/jrebel_workspace_kepler/petclinic/src/main/webapp/WEB-INF/petclinic-servlet.xml'.
2014-08-29 15:26:12 JRebel: Reloading class 'org.springframework.samples.petclinic.web.AddOwnerForm'.
2014-08-29 15:26:12 JRebel: WARN Class 'org.springframework.samples.petclinic.web.AddOwnerForm' has a new non-static field 'mybean', it will be null on existing instances
2014-08-29 15:26:12 JRebel: Destroying singleton for re-registering: org.springframework.web.servlet.handler.MappedInterceptor#0
2014-08-29 15:26:12 JRebel: Adding bean 'org.springframework.web.servlet.handler.MappedInterceptor#0'
2014-08-29 15:26:12 JRebel: Adding bean 'namebean'
2014-08-29 15:26:12 JRebel: Reconfiguring bean 'addOwnerForm' [org.springframework.samples.petclinic.web.AddOwnerForm]
2014-08-29 15:26:12 JRebel: Reconfiguring bean 'addPetForm' [org.springframework.samples.petclinic.web.AddPetForm]
2014-08-29 15:26:12 JRebel: Reconfiguring bean 'addVisitForm' [org.springframework.samples.petclinic.web.AddVisitForm]
2014-08-29 15:26:12 JRebel: Reconfiguring bean 'clinicController' [org.springframework.samples.petclinic.web.ClinicController]
2014-08-29 15:26:12 JRebel: Reconfiguring bean 'editOwnerForm' [org.springframework.samples.petclinic.web.EditOwnerForm]
2014-08-29 15:26:12 JRebel: Reconfiguring bean 'editPetForm' [org.springframework.samples.petclinic.web.EditPetForm]
2014-08-29 15:26:12 JRebel: Reconfiguring bean 'findOwnersForm' [org.springframework.samples.petclinic.web.FindOwnersForm]

We can see that now, without a redeploy, the form loads and the first name field is pre-filled with ZeroTurnaround:

Thanks to JRebel, our developer avoided the entire build, redeploy, restart cycle and was able to successfully add and wire the new bean and can move on to their next task more quickly.