日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品高清无码在线观看 | 亚洲国产综合网 | 久久亚洲精品小早川怜子 | 欧美日韩一本 | 成人精品毛片 | 精品国产乱码久久久久久108 | 女同av网站 | 自拍偷拍亚洲区 | 美女毛片视频 | 欧美视频亚洲视频 | 天天综合网天天综合 | 日韩国产欧美在线视频 | 国产精品一二三四五区 | 精品国产乱码久久久久久1区2区 | 91九色在线视频 | 婷婷国产 | 日韩精品一区二区亚洲av | 黑人狂躁日本妞hd | 欧美一a一片一级一片 | 男人天堂999| 日韩中文字幕国产 | 亚洲黄色网络 | 亚洲一区二区三区播放 | 欧美大片在线看免费观看 | 五月综合激情网 | 日韩av无码一区二区三区不卡 | 日本少妇ⅹxxxxx视频 | 亚洲欧美日韩一区在线观看 | 激情伦成人综合小说 | 99热热99 | 亚洲无码精品国产 | 午夜精品视频一区 | 亚洲成人tv | 欧美日韩在线观看免费 | 久久久无码精品亚洲国产 | 日韩中字在线 | 中文字幕欧美视频 | 黄色小视屏 | 高潮av| av手机版 | 久久精品丝袜高跟鞋 | 日本乱偷人妻中文字幕在线 | 老牛嫩草二区三区观影体验 | 国产无套粉嫩白浆内谢 | 日韩永久免费视频 | 成人性视频免费网站 | 久久伊人中文字幕 | 人妻无码一区二区三区免费 | av最新网| 国产日韩精品一区二区三区 | 亚洲av无码一区二区三区在线播放 | 色婷婷视频在线观看 | 色爱视频 | 波多野结衣一区二区三区在线 | 神马久久久久久久久久 | 亚洲自拍三区 | 国产乱子伦精品 | 五月天婷婷丁香花 | 黑人多p混交群体交乱 | 欧美视频直播网站 | 亚洲午夜久久久久久久久久久 | 成人国产精品久久久网站 | 人人干免费 | 精品国产a | 日日干,夜夜操 | xx色综合 | 人人干天天干 | 亚洲激情图片区 | 久久青 | 国产日韩在线视频 | 成人淫片 | 狠狠草视频 | 日本老师巨大bbw丰满 | 少妇又色又爽又高潮极品 | 一区二区欧美精品 | 欧美日本一区 | 太久av| 激情视频区 | 毛片库| 亚洲一级在线 | 国产日韩一区二区 | 久久高清无码视频 | 中文字幕巨乳 | 完美搭档在线观看 | 西野翔之公侵犯中文字幕 | 国产传媒一级片 | 麻豆区1免费 | 亚洲av永久无码精品三区在线 | 无码日本精品xxxxxxxxx | 在线va视频| 特黄一区二区 | 台湾无码一区二区 | 久久天天干 | 久久99网 | 国产一级大片在线观看 | 免费久草视频 | 奇米精品一区二区三区四区 | 日本91网站| 欧美激情五月 |