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

歡迎訪問 生活随笔!

生活随笔

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

javascript

在Spring容器外部连接对象依赖项

發(fā)布時間:2023/12/3 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Spring容器外部连接对象依赖项 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
有幾種有趣的方法可以設(shè)置在Spring容器外部實例化的對象的屬性和依賴關(guān)系。

用例首先,為什么我們需要在Spring容器之外進行依賴注入–我知道三個用例,其中我實例化了Spring容器之外的對象并需要注入依賴。

首先考慮使用Spring TaskExecutor執(zhí)行一系列任務(wù)的情況,下面突出顯示的任務(wù)在Spring容器外部實例化:

List<Callable<ReportPart>> tasks = new ArrayList<Callable<ReportPart>>();List<ReportRequestPart> reportRequestParts = reportRequest.getRequestParts();for (ReportRequestPart reportRequestPart : reportRequestParts) {tasks.add(new ReportPartRequestCallable(reportRequestPart, reportPartGenerator));}List<Future<ReportPart>> responseForReportPartList;List<ReportPart> reportParts = new ArrayList<ReportPart>();try {responseForReportPartList = executors.invokeAll(tasks);for (Future<ReportPart> reportPartFuture : responseForReportPartList) {reportParts.add(reportPartFuture.get());}} catch (Exception e) {logger.error(e.getMessage(), e);throw new RuntimeException(e);}public class ReportPartRequestCallable implements Callable<ReportPart> {private final ReportRequestPart reportRequestPart;private final ReportPartGenerator reportPartGenerator;public ReportPartRequestCallable(ReportRequestPart reportRequestPart, ReportPartGenerator reportPartGenerator) {this.reportRequestPart = reportRequestPart;this.reportPartGenerator = reportPartGenerator;}@Overridepublic ReportPart call() {return this.reportPartGenerator.generateReportPart(reportRequestPart);} }

第二個用例是ActiveRecord模式,說一下Spring Roo附帶的示例,請考慮以下方法,其中Pet類需要自身持久化并需要實體管理器來執(zhí)行此操作:

@Transactionalpublic void Pet.persist() {if (this.entityManager == null) this.entityManager = entityManager();this.entityManager.persist(this);}

第三種用例是針對標記庫,該標記庫由Web容器實例化,但需要Spring的一些依賴。

解決方案 1.第一種方法實際上很簡單,即通過構(gòu)造函數(shù)或設(shè)置器在對象實例化時提供依賴項。 這是我在第一個用例中使用的內(nèi)容,在第一個用例中,任務(wù)具有兩個依賴關(guān)系,這些依賴關(guān)系由實例化任務(wù)的服務(wù)提供:

tasks.add(new ReportPartRequestCallable(reportRequestPart, reportPartGenerator));

2.第二種方法是創(chuàng)建一個知道Spring容器的工廠,聲明容器內(nèi)的原型作用域所需的bean,并通過應(yīng)用程序上下文的getBeans方法獲取這些bean,

將bean聲明為原型作用域bean:

<bean name='reportPartRequestCallable' class='org.bk.sisample.taskexecutor.ReportPartRequestCallable' scope='prototype'><property name='reportPartGenerator' ref='reportPartGenerator'></property></bean><bean name='reportPartRequestCallableFactory' class='org.bk.sisample.taskexecutor.ReportPartRequestCallableFactory'/>

和提供豆子的工廠:

public class ReportPartRequestCallableFactory implements ApplicationContextAware{private ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}public ReportPartRequestCallable getReportPartRequestCallable(){return this.applicationContext.getBean('reportPartRequestCallable', ReportPartRequestCallable.class);} }

3.第三種方法是上述方法的一種變體,它是實例化bean,然后使用AutoWireCapableBeanFactory.autowireBean(instance)注入依賴項,方法是:

public class ReportPartRequestCallableFactory implements ApplicationContextAware{private GenericApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = (GenericApplicationContext)applicationContext;}public ReportPartRequestCallable getReportPartRequestCallable(){ReportPartRequestCallable reportPartRequestCallable = new ReportPartRequestCallable();applicationContext.getBeanFactory().autowireBean(reportPartRequestCallable);return reportPartRequestCallable;} }

4.第四種方法是使用@Configurable ,但要注意的是它需要AspectJ才能工作。 Spring從本質(zhì)上增強了類的構(gòu)造函數(shù),以按照上面第三種方法中明確完成的方式注入依賴項:

import org.springframework.beans.factory.annotation.Configurable;@Configurable('reportPartRequestCallable') public class ReportPartRequestCallable implements Callable<ReportPart> {private ReportRequestPart reportRequestPart;@Autowired private ReportPartGenerator reportPartGenerator;public ReportPartRequestCallable() {}@Overridepublic ReportPart call() {return this.reportPartGenerator.generateReportPart(reportRequestPart);}public void setReportRequestPart(ReportRequestPart reportRequestPart) {this.reportRequestPart = reportRequestPart;}public void setReportPartGenerator(ReportPartGenerator reportPartGenerator) {this.reportPartGenerator = reportPartGenerator;} }

還需要以下內(nèi)容來配置負責@Configurable編織的Aspect:

<context:spring-configured/>

完成這些更改后,Spring會處理使用@Configurable注釋的類的任何依賴關(guān)系,即使構(gòu)造完全在容器外部完成也是如此:

@Overridepublic Report generateReport(ReportRequest reportRequest) {List<Callable<ReportPart>> tasks = new ArrayList<Callable<ReportPart>>();List<ReportRequestPart> reportRequestParts = reportRequest.getRequestParts();for (ReportRequestPart reportRequestPart : reportRequestParts) {ReportPartRequestCallable reportPartRequestCallable = new ReportPartRequestCallable(); reportPartRequestCallable.setReportRequestPart(reportRequestPart);tasks.add(reportPartRequestCallable);}.......

結(jié)論

所有上述方法都有效地注入了在容器外部實例化的對象中的依賴項。 我個人更喜歡在有AspectJ支持的情況下使用方法4(使用@Configurable),否則我會使用方法2(隱藏在工廠后面并使用原型bean)。

祝您編程愉快,別忘了分享!

參考: all和其他博客中來自JCG合作伙伴 Biju Kunjummen的方法,用于連接Spring容器外部對象的依賴關(guān)系 。


翻譯自: https://www.javacodegeeks.com/2012/09/wire-object-dependencies-outside-spring.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的在Spring容器外部连接对象依赖项的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。