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

主站蜘蛛池模板: 99re在线国产 | 在线观看99 | 97视频人人 | 色偷av| 夜夜爽网站 | 熟女肥臀白浆大屁股一区二区 | 一卡二卡国产 | 中日韩精品视频在线观看 | 久久毛片网站 | 97成人精品 | 香港黄色网 | 双腿张开被9个男人调教 | 国产麻豆一区二区三区在线观看 | 亚洲色图视频在线观看 | 免费黄色大片网站 | 伊人精品影院 | 国产精品免费一区二区 | 幸福宝在线观看 | 春色伊人| 久热精品视频在线观看 | 97插插插| 久九九| 国产成人精品视频在线观看 | 超碰在线观看97 | 天天操网站 | 国产女人叫床高潮大片免费 | 日本一区二区三区视频免费看 | www.狠狠撸.com | 色综合久久88色综合天天免费 | 国产精品视频一区二区在线观看 | 久久91av | 老熟妇高潮一区二区高清视频 | 成人免费av网站 | 日韩激情一区二区 | 国产按摩一区二区三区 | 国产69久久| 91资源在线视频 | 午夜激情久久 | 国产无遮无挡120秒 欧美综合图片 | 激情综合五月天 | 四虎精品成人免费网站 | 日本一区视频在线播放 | 亚洲色图p | 少妇脱了内裤让我添 | 五月天激情小说 | 青青青免费在线视频 | 成年人爱爱视频 | 欧美日韩在线播放 | 激情a| 小箩莉末发育娇小性色xxxx | 国产最爽的乱淫视频国语对白 | 人人艹人人 | 亚洲a免费| 日韩精品二区在线观看 | 欧美日一区二区三区 | 精品国产av一区二区 | 中文字幕无线码一区 | 亚洲一区二区三区成人 | 四虎影视成人永久免费观看亚洲欧美 | 亚洲精品一区久久久久久 | 小泽玛利亚一区二区三区在线观看 | 国产视频二| av在线播放器 | 亚洲激情综合网 | 吻胸摸激情床激烈视频 | 日韩精品一区二区三区在线观看 | 无码成人一区二区 | 亚洲综合免费 | 免费在线观看一区二区 | 日韩激情综合网 | 在线观看中文字幕一区二区 | 丁香花电影免费播放在线观看 | 人人综合网| 国产精品久久久久高潮 | 日本欧美www | 最新免费av| 久久久亚洲国产精品 | 午夜精品视频在线 | 亚洲黄网av | 天天摸夜夜添狠狠添婷婷 | 成人午夜在线视频 | 人妻一区二区三区四区五区 | 国产又粗又猛又爽69xx | 黄色a级片| 亚洲人人夜夜澡人人爽 | 韩国三级丰满少妇高潮 | 女生喷水视频 | 天堂中文在线资源 | 深夜免费视频 | 亚洲激情五月 | 全部毛片永久免费看 | 久久久久久18 | 黄色av一区 | 亚洲黄色a级片 | 日韩精品第1页 | 日韩一区二区免费在线观看 | 暴力调教一区二区三区 | 成人性视频免费网站 | 国产又黄又猛又爽 |