Bridging Configuration from Spring to Legacy Frameworks

Bridging Configuration from Spring to Legacy Frameworks

he Spring framework and its Plain Old Java Object (POJO)-based programming model enable you to inject collaborative objects into your classes via setter methods instead of looking them up inline via factory or locator classes. This approach is not only more intuitive, but it also drastically reduces the amount of code you need to write. In fact, Spring’s POJO-based programming model has proven to be a much better way to write, test, and assemble robust Java EE applications. But it works only if you configure your objects with Spring.

What happens when you combine Spring with an in-house legacy framework or Java EE technology such as EJBs or servlets? Suppose you have some objects defined outside of Spring (via your legacy services configuration), while others are defined and managed inside the Spring container. Can you extend Spring’s configuration to simplify and standardize your legacy framework? Maybe the biggest problem you face in trying to do so is that Spring and your legacy framework have competing bootstrapping processes. The Spring IOC container has a start-up process defined by the creation of a BeanFactory, and your legacy framework has its own rules for starting up servers.

The lazy solution would be to load Spring the first time a legacy service needs to refer to a bean. As an example of this approach, the following listing shows a typical combination of a legacy framework with Spring:

public class MyLegacyService extends SomePreIOC_AbstractService {    Pojo1 pojo1;    Pojo2 pojo2;    public void setPojo1(Pojo1 p1) {   pojo1 = p1;    }    public void setPojo2(Pojo2 p2) {   pojo2 = p2;    }    public MyLegacyService(String serviceName) {   super(serviceName);    }    // some pre-dependency injection configure method     // called by the legacy framework.    public void configure(ServiceConfig config) {   super.configure(config);   setPojo1((Pojo1) SpringSingleton.                    getBeanFactory().                    getBean("pojo.1"));   setPojo2((Pojo2) SpringSingleton.                    getBeanFactory().                    getBean("pojo.2"));    }}

The service MyLegacyService is created outside of Spring but needs to refer to Spring managed beans. Notice the explicit calls to access the specific Spring beans pojo.1 and pojo.2.

The following is the corresponding Spring configuration:

     "http://www.springframework.org/dtd/spring-beans.dtd">          

One subtle problem with this approach is that it is not clear how Spring managed objects can refer to your legacy service. Typically in Spring, you access objects created outside of the IOC container with a FactoryBean. This works extremely well for objects in a JNDI context, because the JNDI context typically will exist and be available prior to Spring starting. In the case of a legacy framework, however, you may not be able to guarantee that a FactoryBean will obtain an instance to an initialized legacy service. As an example, imagine the bean pojo.1 also needs a reference to its associated instance of MyLegacyService.

An ideal solution would be a configuration mechanism that supports your cyclical dependencies (MyLegacyService depending on Pojo1, and Pojo1 depending on MyLegacyService), yet remains flexible so that it doesn’t matter which bootstrapping process starts first. In other words, you wouldn’t need to care whether the external service is created first or it is referenced in Spring first.

The Oranjestad Spring project defines just such an extension to Spring. It allows you to register your legacy objects so that they can be configured via Spring. It does this using Spring’s namespaces extension point (see Sidebar 1. How Do External Beans Work?).

To use Oranjestad Spring, you first include an external namespace, which requires you to remove the standard DTD reference and import the external namespace in the beans tag:

xmlns:external="http://oranjestad.sourceforge.net/schema/external"  xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://oranjestad.sourceforge.net/schema/external
http://oranjestad.sourceforge.net/schema/external/external_1.0.xsd">

Then you add a Spring-style bean definition to define your configuration. The definition will be applied to your external legacy service:

<external:bean id="some.service" class="MyLegacyService">            external:bean>

Other than the external namespace qualifier, the revised XML configuration looks and acts exactly like a standard Spring definition and allows you to define your circular reference between the external legacy service (referred to as some.service in the Spring file) and pojo.1:

 id="some.service" class="MyLegacyService">                ref="some.service" /> 

The only other detail to cover is how your legacy service registers itself so that the Spring configuration can be applied to it. You accomplish this by using the oranjestad class SpringConfigurationBridge, which is called by the service after it has been initialized:

import oranjestad.spring.bootstrap.SpringConfigurationBridge;public class MyLegacyService extends SomePreIOC_AbstractService {    Pojo1 pojo1;    Pojo2 pojo2;    public void setPojo1(Pojo1 p1) {   pojo1 = p1;    }    public void setPojo2(Pojo2 p2) {   pojo2 = p2;    }    public MyLegacyService(String serviceName) {   super(serviceName);    }    // some pre-dependency injection configure method     // called by the legacy framework.    public void configure(ServiceConfig config) {   super.configure(config);	   SpringConfigurationBridge.            configure(this, "some.service");    }}

With the external bean approach, you have much greater flexibility with services that get created in an intertwined fashion. External beans do the following for your legacy system:

  • Handle all combinations of the legacy services starting before or after Spring
  • Extend dependency injection to your legacy system
  • Add some automatic annotation-based capabilities such as Spring’s JMX annotations for creating MBeans to work with your legacy service

This approach can greatly increase the adoption of Spring into a legacy framework by eliminating ugly bean lookups from the legacy service as well as by moving configuration from the legacy system to Spring.

devx-admin

devx-admin

Share the Post:
Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023,

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed

Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at the Lubiatowo-Kopalino site in Pomerania.

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will result in job losses. However,

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023, more than one-fifth of automobiles

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed are at the forefront because

Sunsets' Technique

Inside the Climate Battle: Make Sunsets’ Technique

On February 12, 2023, Luke Iseman and Andrew Song from the solar geoengineering firm Make Sunsets showcased their technique for injecting sulfur dioxide (SO₂) into the stratosphere as a means

AI Adherence Prediction

AI Algorithm Predicts Treatment Adherence

Swoop, a prominent consumer health data company, has unveiled a cutting-edge algorithm capable of predicting adherence to treatment in people with Multiple Sclerosis (MS) and other health conditions. Utilizing artificial

Personalized UX

Here’s Why You Need to Use JavaScript and Cookies

In today’s increasingly digital world, websites often rely on JavaScript and cookies to provide users with a more seamless and personalized browsing experience. These key components allow websites to display

Geoengineering Methods

Scientists Dimming the Sun: It’s a Good Thing

Scientists at the University of Bern have been exploring geoengineering methods that could potentially slow down the melting of the West Antarctic ice sheet by reducing sunlight exposure. Among these

why startups succeed

The Top Reasons Why Startups Succeed

Everyone hears the stories. Apple was started in a garage. Musk slept in a rented office space while he was creating PayPal with his brother. Facebook was coded by a

Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as opposed to the 176% return

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024 approaches, the industry seems to

Elevated Content Deals

Elevate Your Content Creation with Amazing Deals

The latest Tech Deals cater to creators of different levels and budgets, featuring a variety of computer accessories and tools designed specifically for content creation. Enhance your technological setup with

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security. These carefully designed learning courses

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers to better manage smaller unmanned

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like artificial intelligence (AI), semiconductors, and

Huge Savings

Score Massive Savings on Portable Gaming

This week in tech bargains, a well-known firm has considerably reduced the price of its portable gaming device, cutting costs by as much as 20 percent, which matches the lowest

Cloudfare Protection

Unbreakable: Cloudflare One Data Protection Suite

Recently, Cloudflare introduced its One Data Protection Suite, an extensive collection of sophisticated security tools designed to protect data in various environments, including web, private, and SaaS applications. The suite

Drone Revolution

Cool Drone Tech Unveiled at London Event

At the DSEI defense event in London, Israeli defense firms exhibited cutting-edge drone technology featuring vertical-takeoff-and-landing (VTOL) abilities while launching two innovative systems that have already been acquired by clients.

2D Semiconductor Revolution

Disrupting Electronics with 2D Semiconductors

The rapid development in electronic devices has created an increasing demand for advanced semiconductors. While silicon has traditionally been the go-to material for such applications, it suffers from certain limitations.

Cisco Growth

Cisco Cuts Jobs To Optimize Growth

Tech giant Cisco Systems Inc. recently unveiled plans to reduce its workforce in two Californian cities, with the goal of optimizing the company’s cost structure. The company has decided to

FAA Authorization

FAA Approves Drone Deliveries

In a significant development for the US drone industry, drone delivery company Zipline has gained Federal Aviation Administration (FAA) authorization, permitting them to operate drones beyond the visual line of

Mortgage Rate Challenges

Prop-Tech Firms Face Mortgage Rate Challenges

The surge in mortgage rates and a subsequent decrease in home buying have presented challenges for prop-tech firms like Divvy Homes, a rent-to-own start-up company. With a previous valuation of

Lighthouse Updates

Microsoft 365 Lighthouse: Powerful Updates

Microsoft has introduced a new update to Microsoft 365 Lighthouse, which includes support for alerts and notifications. This update is designed to give Managed Service Providers (MSPs) increased control and

Website Lock

Mysterious Website Blockage Sparks Concern

Recently, visitors of a well-known resource website encountered a message blocking their access, resulting in disappointment and frustration among its users. While the reason for this limitation remains uncertain, specialists