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

Using Spring MVC with JRebel: Refactoring XML config files 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 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.

In this next post in the JRebel with Spring MVC series we talk about Spring application configuration. Historically Spring configuration has been done with XML files, in the last few years annotations and configuration classes are becoming more common. Spring’s XML configuration files allow developers to define beans, how they are wired together, and what is injected into them.

A big knock against Spring’s traditional XML configuration methodology is unwieldy massive XML files that can’t be read, tested, and validated as easily as code. Most developers can remember the days of gigantic applicationContext.xml files that encompassed all of your application’s Spring configuration. One technique to make handling the XML files more palatable is to use import statements and create a hierarchy of XML files that reference each other. This is a pretty standard technique that devs that use Ant have been doing for years to help modularize their build files.

In this blog post, we’ll refactor one of our bean definitions from our servlet’s XML file into a separate XML file, and because we’ll be using JRebel, we won’t have to rebuild, redeploy, or restart our application server.

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 add a new bean to the Spring petclinic application in the petclinic-servlet.xml file like this:

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

When this bean is injected, we can pull the value from name and get “ZeroTurnaround.” We are using the bean here in our AddOwnerForm class:

@Autowired
MyNameBean mybean;

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

This is what it looks like in the application:
Screenshot 2014-09-18 16.58.28

Now, let’s say we wanted to separate out the bean definition for our namebean, let’s create a new mybean.xml with the same definition but a different value being inject:

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

Then, in the petclinic-servlet.xml, we replace our previous definition with an import statement:

<import resource="mybean.xml" />

Now, with JRebel, all we have to do is refresh our page in our browser and we’ll see the new value, “JRebel!” instead of “ZeroTurnaround”:
Screenshot 2014-09-18 17.29.38

and here is the console output showing that JRebel successfully reloading the spring bean definitions and also is now monitoring our new XML file:

2014-09-18 17:29:33 JRebel-Spring: Reloading Spring bean definitions in 'file:/Users/koblentz/demo_materials/workspaces/jrebel_workspace_kepler/petclinic/src/main/webapp/WEB-INF/petclinic-servlet.xml'.
2014-09-18 17:29:33 JRebel-Spring: Destroying singleton for re-registering: org.springframework.web.servlet.handler.MappedInterceptor#0
2014-09-18 17:29:33 JRebel-Spring: Monitoring Spring bean definitions in '/Users/koblentz/demo_materials/workspaces/jrebel_workspace_kepler/petclinic/src/main/webapp/WEB-INF/mybean.xml'.
2014-09-18 17:29:33 JRebel-Spring: Adding bean 'org.springframework.web.servlet.handler.MappedInterceptor#0'
2014-09-18 17:29:33 JRebel-Spring: Reconfiguring bean 'addOwnerForm' [org.springframework.samples.petclinic.web.AddOwnerForm]
2014-09-18 17:29:33 JRebel-Spring: Reconfiguring bean 'addPetForm' [org.springframework.samples.petclinic.web.AddPetForm]
2014-09-18 17:29:33 JRebel-Spring: Reconfiguring bean 'addVisitForm' [org.springframework.samples.petclinic.web.AddVisitForm]
2014-09-18 17:29:33 JRebel-Spring: Reconfiguring bean 'clinicController' [org.springframework.samples.petclinic.web.ClinicController]
2014-09-18 17:29:33 JRebel-Spring: Reconfiguring bean 'editOwnerForm' [org.springframework.samples.petclinic.web.EditOwnerForm]
2014-09-18 17:29:33 JRebel-Spring: Reconfiguring bean 'editPetForm' [org.springframework.samples.petclinic.web.EditPetForm]
2014-09-18 17:29:33 JRebel-Spring: Reconfiguring bean 'findOwnersForm' [org.springframework.samples.petclinic.web.FindOwnersForm]
2014-09-18 17:29:33 JRebel-Spring: Reconfiguring bean 'namebean' [org.springframework.samples.petclinic.MyNameBean]

Thanks to JRebel, our developer avoided the entire build, redeploy, restart cycle and was able to successfully refactor their Spring configuration files and can move on to their next task more quickly.


SET UP JREBEL IN THE NEXT 10 MIN!

 

  • rstoyanchev

    JRebel is a great product and this is a good post but the comment about massive unwieldy XML config is a reference to the days before Spring MVC annotations were introduced in Spring 2.5 going back to 2008 which is also how old that StackOverflow thread is!

  • arhan

    Yes and no. There’s plenty of projects that are still using the older versions of Spring. And from JRebel point of view it doesn’t change anything as XML and annotations are both a declarative way to configure the behaviour of the framework. Thus JRebel integrates with XML configs as well as it keeps track of framework specific annotations.

  • Let your loved ones residing in Hyderabad think only about you by sending them these attractive Gifts and Flower arrangements which
    will add a pleasant charm to their life. Send Gifts to Hyderabad to your loved ones through different online shopping portals and cheer them up.