第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

自注入原型bean——循環(huán)引用

自注入原型bean——循環(huán)引用

白板的微信 2021-06-20 12:15:37
我需要自我注入原型bean。據(jù)我所知,如果 bean scope="singleton" 是可能的,但在這種情況下,我從 spring 收到消息:“應(yīng)用程序上下文中某些 bean 的依賴(lài)關(guān)系形成一個(gè)循環(huán):postMan2”我的豆子:@Service@Scope("prototype")public class PostMan2 implements PostMans2 {    private PostMans2 postman;    @Async    public Future<String> deliverLetter(String message, int i) {        postman.test();        String res = "result!";        return new AsyncResult<String>(res);    }    @Override    public void test() {        System.out.println("Self injection example thread name="+name);    }    @PostConstruct    private void init() {        postman = ctx.getBean(PostMans2.class);    }}調(diào)用:@Servicepublic class PostOffice implements PostOffices {    @Autowired    ApplicationContext ctx;    @Override    public void creatingPostmans() {        PostMans2 thr = ctx.getBean(PostMans2.class);        Future<String> fut = thr.deliverLetter("Some letter", 100);        while (!fut.isDone()) {           Thread.sleep(1000);        }        System.out.println("ending of PostMan's jobs...");    }}如何改進(jìn)我的代碼?
查看完整描述

2 回答

?
胡子哥哥

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊

我認(rèn)為你init()正在形成一個(gè)循環(huán)。


當(dāng)你在PostOffice課堂上調(diào)用它時(shí)


PostMans2 thr = ctx.getBean(PostMans2.class);

PostMans2 類(lèi)將被引用。


在PostMans2你已經(jīng)定義了init()哪個(gè)將再次引用PostMans2,這將繼續(xù)


因此,嘗試消除init()從PostMan2所有應(yīng)該罰款


@PostConstruct

private void init() {

    postman = ctx.getBean(PostMans2.class);

}


查看完整回答
反對(duì) 回復(fù) 2021-06-23
?
米琪卡哇伊

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊

為什么你需要通過(guò) Spring 來(lái)獲得這個(gè)實(shí)例?


看起來(lái)你想要這樣做:


@PostConstruct

private void init() {

    postman = this;

}


查看完整回答
反對(duì) 回復(fù) 2021-06-23
  • 2 回答
  • 0 關(guān)注
  • 170 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)