javascript
在非托管对象中使用Spring托管Bean
即使我們想使用現(xiàn)有的最佳和最新技術,我們也必須處理遺留代碼。 想象一下,新代碼是用Spring框架的最新技術編寫的,而舊代碼根本不是用Spring編寫的。 然后在非托管Spring對象中使用Spring托管Bean是我們必須處理的模式之一。 遺留代碼具有非托管的Spring對象,而我們要引用的代碼是Spring托管的Bean。 我們?nèi)绾谓鉀Q這個問題?
創(chuàng)建一個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。 我們創(chuàng)建了一個名為getTaxService的方法來返回剛剛創(chuàng)建的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
接下來,我們?yōu)镾pringContextBridgedServices接口創(chuàng)建一個實現(xiàn)。 讓我們將此類SpringContextBridge ,使其成為Spring Bean,并在該類中添加以下功能。
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:有可能以靜態(tài)方法本身返回Spring托管的bean。 我選擇不這樣做,因此我的靜態(tài)方法較少,以后可以模擬一些參考服務。
- 注2:最終,您希望將這兩種功能分開。 一個持有ApplicationContext并返回SpringContextBridgedServices Bean。 另一個是SpringContextBridgedServices Bean本身。 在這個簡短的演示中,我只是將它們放在同一個Bean中。
帶我去橋
現(xiàn)在是時候打電話給這座橋了。 就像下面的代碼所示的那樣簡單。
傳統(tǒng)對象
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); }現(xiàn)在我們可以通過調(diào)用SpringContextBridge.services().getService(TaxService.class)獲得服務。 在這種替代方案中,我們無法控制可以橋接哪個Spring托管Bean。
翻譯自: https://www.javacodegeeks.com/2015/03/using-spring-managed-bean-in-non-managed-object.html
總結
以上是生活随笔為你收集整理的在非托管对象中使用Spring托管Bean的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海松茸的做法 海松茸如何做
- 下一篇: Spring项目中的Netflix Ar