Home » Java » Enterprise Java » Integrating Spring Into Legacy Applications

About Roger Hughes

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;

  public String toString() {
    return "The toString()";

  public LegacyAppClass getInjectedBean() {
    return injectedBean;

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

  public void myDoSomethingMethod() {


…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.

  public void loadExternalClassTest2() {

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

    parentContext.refresh(); // seems to be required sometimes

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

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



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.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"
       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"/>

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....


Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.