将Spring集成到旧版应用程序中

所有Spring开发人员喜欢做的事情之一就是将Spring塞入他们正在工作的任何应用程序中–这是我生活中的罪恶感之一:您看到一些代码,认为它是垃圾,因为它包含几个众所周知的反模式,然后想想如果这个应用程序是Spring应用程序会多么酷。

使用旧版代码时,您无法在一夜之间将其转换为功能完善的Spring应用程序。 您需要做的是一次添加一点Spring代码:一步一步地完成,有一种很好的方法。

在以下场景中,您正在处理一些旧代码,并且编写了一个名为: MySpringBean的Spring bean,它需要使用旧类: LegacyAppClass

遗留类如下所示:

public class LegacyAppClass {
  // some old code goes here

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

…虽然您的新SpringBean如下所示:

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();
  }

}

…如您所见, myDoSomethingMethod ()方法需要调用旧版legacyDoSomethingMethod ()方法。

鉴于任何遗留应用程序都有其创建各种对象的方式,并且您的新Spring代码将需要使用这些对象来完成其工作,那么您需要一种将遗留对象与闪亮的新对象组合的方式。 这通常涉及将遗留对象添加到您的Spring Context中,并将它们注入到您的对象中,为此,您需要Spring的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());
  }

在上面的测试代码中,要注意的第一点是,我创建了一个供测试使用的LegacyAppClass实例,但是在实际应用中,这已经在您的旧代码库中的某个位置创建了。 接下来的三行是魔术发生的地方……

GenericApplicationContext parentContext = new StaticApplicationContext();

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

…在上面的代码段中,您可以看到我正在创建一个StaticApplicationContext ,然后向其中实用地添加了我的旧类实例。

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

如上所示,最后的任务是使用适合您的项目的任何方法来创建新的Spring应用程序上下文。 在这种情况下,我使用了众所周知的ClassPathXmlApplicationContext,但其他类型的应用程序上下文也可以正常工作。

您可能会说这是一个简单的Micky-Mouse示例,但是从经验来看,它的扩展性很好。 作为Martin Fowler的Strangler Pattern实现的一部分,目前有两个完整的旧式JSP Front Strategy MVC应用程序正在使用它(在我于去年10月的博客中详细介绍了它,名为Everybody Knows About MVC )。

最后,出于完整性考虑,下面是此示例的XML配置:

<?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>

参考: JCG合作伙伴 Roger Hughes的 将Spring集成到旧版应用程序中  Captain Debug的Blog中


翻译自: https://www.javacodegeeks.com/2012/03/integrating-spring-into-legacy.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值