日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2023/12/3 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将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 doesn't 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 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

總結

以上是生活随笔為你收集整理的将Spring集成到旧版应用程序中的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。