菜鸟建网站,wordpress主题如何更换,photoshop安卓版,网站开发服务税收编码所有Spring开发人员喜欢做的事情之一就是将Spring塞入他们正在工作的任何应用程序中–这是我生活中的罪恶感之一#xff1a;您看到一些代码#xff0c;认为它是垃圾#xff0c;因为它包含几个众所周知的反模式#xff0c;然后想想如果这个应用程序是Spring应用程序会多么酷… 所有Spring开发人员喜欢做的事情之一就是将Spring塞入他们正在工作的任何应用程序中–这是我生活中的罪恶感之一您看到一些代码认为它是垃圾因为它包含几个众所周知的反模式然后想想如果这个应用程序是Spring应用程序会多么酷。 使用旧版代码时您无法在一夜之间将其转换为功能完善的Spring应用程序。 您需要做的是一次添加一点Spring代码一步一步地完成有一种很好的方法。 在以下场景中您正在处理一些旧代码并且编写了一个名为 MySpringBean的Spring bean它需要使用旧类 LegacyAppClass 遗留类如下所示 public class LegacyAppClass {// some old code goes herepublic void legacyDoSomethingMethod() {System.out.println(This is so old it doesnt use a logger....);}
} …虽然您的新SpringBean如下所示 public class MySpringBean {private LegacyAppClass injectedBean;Overridepublic 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 。 Testpublic void loadExternalClassTest2() {LegacyAppClass myInstance new LegacyAppClass();GenericApplicationContext parentContext new StaticApplicationContext();parentContext.getBeanFactory().registerSingleton(injectedBean,myInstance);parentContext.refresh(); // seems to be required sometimesApplicationContext 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 version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdbean idmySpringBean classmiscillaneous.springintegration.MySpringBeanproperty nameinjectedBean refinjectedBean//bean
/beans 参考 JCG合作伙伴 Roger Hughes的 将Spring集成到旧版应用程序中 在Captain Debug的Blog中 。 翻译自: https://www.javacodegeeks.com/2012/03/integrating-spring-into-legacy.html