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

歡迎訪問 生活随笔!

生活随笔

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

javascript

在非托管对象中使用Spring托管Bean

發布時間:2023/12/3 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在非托管对象中使用Spring托管Bean 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

即使我們想使用現有的最佳和最新技術,我們也必須處理遺留代碼。 想象一下,新代碼是用Spring框架的最新技術編寫的,而舊代碼根本不是用Spring編寫的。 然后在非托管Spring對象中使用Spring托管Bean是我們必須處理的模式之一。 遺留代碼具有非托管的Spring對象,而我們要引用的代碼是Spring托管的Bean。 我們如何解決這個問題?

創建一個Spring Bean

假設我們有一個名為TaxService的托管Spring Bean和一個名為LegacyObject的對象。 LegacyObject是遺留代碼,從中可以引用托管Spring Bean上的calculateTax方法。

稅務服務

package com.jdriven;import org.springframework.stereotype.Service;@Service public class TaxServiceImplimplements TaxService {@Overridepublic Double calculateTax(Double price) {return new Double(price * 0.21);} }

與橋接服務方法的接口

我們定義一個包含方法列表的接口。 這些方法中的每一個都返回一個Spring托管Bean。 我們創建了一個名為getTaxService的方法來返回剛剛創建的TaxService Bean。

SpringContextBridgedServices

package com.jdriven;/*** This interface represents a list of Spring Beans (services) which need to be referenced from a non Spring class.*/ public interface SpringContextBridgedServices {TaxService getTaxService(); }

實施Spring Context Bridge

接下來,我們為SpringContextBridgedServices接口創建一個實現。 讓我們將此類SpringContextBridge ,使其成為Spring Bean,并在該類中添加以下功能。

  • 此類還應實現Spring的ApplicationContextAware接口。 我們需要從接口實現的方法中唯一的參數是參數ApplicationContext 。 我們將此參數保存在靜態成員變量中。
  • 創建一個靜態方法以返回SpringContextBridgedServices然后讓該方法返回由Spring管理的Bean。 使用applicationContext.getBean(SpringContextBridgedServices.class)返回它。
  • 自動連接TaxService并將其返回到我們需要從SpringContextBridgedServices方法實現的方法中。
  • SpringContextBridge

    package com.jdriven;import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component;/** * Register this SpringContextBridge as a Spring Component. */ @Component public class SpringContextBridge implements SpringContextBridgedServices, ApplicationContextAware {private static ApplicationContext applicationContext;@Autowiredprivate TaxService taxService; //Autowire the TaxService@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}/*** A static method to lookup the SpringContextBridgedServices Bean in * the applicationContext. It is basically an instance of itself, which * was registered by the @Component annotation.** @return the SpringContextBridgedServices, which exposes all the * Spring services that are bridged from the Spring context.*/public static SpringContextBridgedServices services() {return applicationContext.getBean(SpringContextBridgedServices.class);}@Overridepublic TaxService getTaxService() {return taxService; //Return the Autowired taxService} }
    • 注意1:有可能以靜態方法本身返回Spring托管的bean。 我選擇不這樣做,因此我的靜態方法較少,以后可以模擬一些參考服務。
    • 注2:最終,您希望將這兩種功能分開。 一個持有ApplicationContext并返回SpringContextBridgedServices Bean。 另一個是SpringContextBridgedServices Bean本身。 在這個簡短的演示中,我只是將它們放在同一個Bean中。

    帶我去橋

    現在是時候打電話給這座橋了。 就像下面的代碼所示的那樣簡單。

    傳統對象

    package com.jdriven;public class LegacyObject {private Double price;public Double doTheCalculation() {//Get the Service from the BridgeTaxService taxService = SpringContextBridge.services().getTaxService();return taxService.calculateTax(this.price);} }

    靈活但不受限制的替代方案

    這是限制橋接服務列表的一種方式。 僅SpringContextBridgedServices接口中提到的服務將被橋接。 如果您想要一種更靈活但受控制較少的方法,則可以重寫SpringContextBridgedServices 。

    SpringContextBridgedServicesAlternative

    package com.jdriven;public interface SpringContextBridgedServicesAlternative {<T> T getService(Class<T> serviceType); }

    現在我們可以通過調用SpringContextBridge.services().getService(TaxService.class)獲得服務。 在這種替代方案中,我們無法控制可以橋接哪個Spring托管Bean。

    翻譯自: https://www.javacodegeeks.com/2015/03/using-spring-managed-bean-in-non-managed-object.html

    總結

    以上是生活随笔為你收集整理的在非托管对象中使用Spring托管Bean的全部內容,希望文章能夠幫你解決所遇到的問題。

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