DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. Integrating Spring Into Legacy Applications

Integrating Spring Into Legacy Applications

Roger Hughes user avatar by
Roger Hughes
·
Mar. 17, 12 · Interview
Like (0)
Save
Tweet
Share
12.45K Views

Join the DZone community and get the full member experience.

Join For Free
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>
Spring Framework application

Published at DZone with permission of Roger Hughes, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Handle Secrets in Docker
  • HTTP vs Messaging for Microservices Communications
  • Reliability Is Slowing You Down
  • Cloud Performance Engineering

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: