Enterprise Java

Integrating Spring Into Legacy Applications

One of the things that all Spring developers like to do is to shoehorn Spring into any application they work on – it’s one of my guilty pleasures in life: you see some code, think it’s rubbish because it contains several well known anti-patterns and then think how cool it would be if this app was a Spring app.

When working with legacy code, you can’t convert it into a fully fledged Spring app over night, that takes time. What you need to do is to add Spring code a little at a time: piece by piece and there’s one good way of doing that.

In the following scenario, you’re working on some legacy code and you’ve written a Spring bean called: MySpringBean and it needs to use the legacy class: LegacyAppClass

The legacy class looks like this:

public class LegacyAppClass {
  // some old code goes here

  public void legacyDoSomethingMethod() {
    System.out.println("This is so old it doesn't use a logger....");
  }
}

…whilst your new SpringBean looks like this:

public class MySpringBean {

  private LegacyAppClass injectedBean;

  @Override
  public String toString() {
    return "The toString()";
  }

  public LegacyAppClass getInjectedBean() {
    return injectedBean;
  }

  public void setInjectedBean(LegacyAppClass injectedBean) {
    this.injectedBean = injectedBean;
  }

  public void myDoSomethingMethod() {
    injectedBean.legacyDoSomethingMethod();
  }

}

…as you can see, the myDoSomethingMethod() method needs to call the legacy legacyDoSomethingMethod() method.

Given that any legacy application will have its own way of creating various objects, and that your new Spring code will need to use those objects to get its job done, then you need a way of combining the legacy objects with your shiny new ones. This will usually involve adding the legacy objects into your Spring Context and injecting them into your objects and to do this you need Spring’s StaticApplicationContext.

@Test
  public void loadExternalClassTest2() {

    LegacyAppClass myInstance = new LegacyAppClass();
    GenericApplicationContext parentContext = new StaticApplicationContext();

    parentContext.getBeanFactory().registerSingleton("injectedBean",
        myInstance);
    parentContext.refresh(); // seems to be required sometimes

    ApplicationContext context = new ClassPathXmlApplicationContext(
        new String[] { "SpringIntegrationExample.xml" }, parentContext);

    MySpringBean mySpringBean = context.getBean(MySpringBean.class);
    assertNotNull(mySpringBean);

    mySpringBean.myDoSomethingMethod();

    System.out.println(mySpringBean.toString());
  }

In the test code above the first point to note is that I create an instance of LegacyAppClass for use by the test, but in a real world app this will have already been created somewhere in your legacy code base. The next three lines is where the magic happens…

GenericApplicationContext parentContext = new StaticApplicationContext();

    parentContext.getBeanFactory().registerSingleton("injectedBean",
        myInstance);
    parentContext.refresh(); // seems to be required sometimes

…in the snippet above, you can see that I’m creating a StaticApplicationContext and then pragmatically adding my legacy class instance to it.

ApplicationContext context = new ClassPathXmlApplicationContext(
        new String[] { "SpringIntegrationExample.xml" }, parentContext);

The final task, as shown above, is to then create a new Spring application context using whatever method is suitable for your project. In this case, I’ve used the proverbial ClassPathXmlApplicationContext but other types of app context work just as well.

You may say that this is a simple Micky-Mouse example, but from experience it does scale very well. It’s currently being used by a couple of full scale old style JSP Front Strategy MVC applications, (covered in detail in my blog from last October called Everybody Knows About MVC), as part of an implementation of Martin Fowler’s Strangler Pattern.

Finally, in the interests of completeness, below is the XML config for this example:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  
 <bean id="mySpringBean" class="miscillaneous.springintegration.MySpringBean">
  <property name="injectedBean" ref="injectedBean"/>
 </bean>
</beans>

Reference: Integrating Spring Into Legacy Applications from our JCG partner Roger Hughes at the Captain Debug’s Blog .

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button