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

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

如何配置 Bean 原型范圍提供程序以在 Bean 創(chuàng)建時(shí)使用會(huì)話信息?

如何配置 Bean 原型范圍提供程序以在 Bean 創(chuàng)建時(shí)使用會(huì)話信息?

胡說(shuō)叔叔 2023-06-21 15:55:43
每次訪問(wèn)代理以獲取實(shí)例時(shí),我都需要提供基于會(huì)話信息的 bean。我怎樣才能做到這一點(diǎn)?現(xiàn)在我嘗試了以下。例如:第一個(gè)類(lèi)定義了一個(gè)會(huì)話范圍的 bean。@Component@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)public class BeanSession implements Serializable {    private Serializable rootState;    public <T extends Serializable> T getRootState() {        return (T) rootState;    }    public void setRootState(Serializable rootState) {        this.rootState = rootState;    }}第二類(lèi)有一些與他們的領(lǐng)域相關(guān)的邏輯,也知道如何提供信息。必須每次都創(chuàng)建 bean,因?yàn)樾畔⒃诰€程處理期間可能會(huì)發(fā)生變化。因此,每次Attribute1訪問(wèn)時(shí),我一定會(huì)得到包含新信息的 bean。@Servicepublic class Attribute1Service {    @Resource    private BeanSession beanSession;    public void setDefaultValue() {        Configuration configuration = beanSession.getRootState();        configuration.getAttribute1().setValue("VALUE 1");    }    @Bean    public Attribute1 attribute1() {        Configuration configuration = beanSession.getRootState();        return configuration.getAttribute1();    }}最后,第三個(gè)類(lèi)聲明attribute1as 依賴(lài)項(xiàng)來(lái)執(zhí)行自己的邏輯。@Servicepublic class Attribute2Service {    @Resource    private BeanSession beanSession;    @Resource    private Processor processor;    @Resource    private Attribute1 attribute1;    public void defineAttribute2() {        Configuration configuration = beanSession.getRootState();        String value = processor.getValue(configuration, attribute1);        configuration.getAttribute2().setValue(value);    }    public void defineAttribute3() {        Configuration configuration = beanSession.getRootState();        String value = processor.getValue(configuration, attribute1);        configuration.getAttribute3().setValue(value);    }}我不想從頭開(kāi)始訪問(wèn)信息attribute1,因?yàn)檫@會(huì)在信息提供者和消費(fèi)者之間造成硬耦合。beanSessionAttribute2Service
查看完整描述

2 回答

?
慕桂英546537

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

異常說(shuō)明了一切 - attribute1 bean 是在應(yīng)用程序初始化期間創(chuàng)建的(通過(guò)會(huì)話作用域 bean),但沒(méi)有與請(qǐng)求綁定的線程。您還應(yīng)該代理您的 attribute1 bean,因?yàn)槟鷮⑵渥⑷氲絾卫▽傩?2 服務(wù)。)



查看完整回答
反對(duì) 回復(fù) 2023-06-21
?
暮色呼如

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

基于 Alexander.Furer 給出的見(jiàn)解。我創(chuàng)建了自己的作用域,并管理它來(lái)調(diào)用 bean 提供者,以便在Attribute1方法的每次訪問(wèn)中都擁有新鮮的 bean。


為此,我擴(kuò)展了以下范圍:


// Register scope as "runtime"

public class RuntimeScope implements Scope {


    @Override

    public Object get(String name, ObjectFactory<?> objectFactory) {

        return objectFactory.getObject();

    }


    ...

}

新Attribute1服務(wù):


@Service

public class Attribute1Service {


    @Resource

    private BeanSession beanSession;


    public void setDefaultValue() {

        Configuration configuration = beanSession.getRootState();

        configuration.getAttribute1().setValue("VALUE 1");

    }


    @Bean

    @Scope(value = "runtime", proxyMode = ScopedProxyMode.TARGET_CLASS)

    public Attribute1 attribute1() {

        Configuration configuration = beanSession.getRootState();

        return configuration.getAttribute1();

    }


}

消費(fèi)者Attribute2服務(wù):


@Service

public class Attribute2Service {


    @Resource

    private BeanSession beanSession;


    @Resource

    private Processor processor;


    @Resource

    private Attribute1 attribute1;


    public void defineAttribute2() {

        Configuration configuration = beanSession.getRootState();

        String value = processor.getValue(configuration, attribute1.getValue()); // Will call Attribute1 service to require the fresh bean

        configuration.getAttribute2().setValue(value);

    }


    public void defineAttribute3() {

        Configuration configuration = beanSession.getRootState();

        String value = processor.getValue(configuration, attribute1.getValue()); // Will call Attribute1 service to require the fresh bean

        configuration.getAttribute3().setValue(value);

    }


}

我沒(méi)有看到的問(wèn)題是 Attribute1 應(yīng)該是處理 bean 實(shí)例化的代理。因此,通過(guò)創(chuàng)建我自己的范圍,我可以保證訪問(wèn) attribute1(由Attribute2Servicewith生成attribute1.getValue())方法將創(chuàng)建一個(gè)新的 bean(由 提供Attribute1Service)。


查看完整回答
反對(duì) 回復(fù) 2023-06-21
  • 2 回答
  • 0 關(guān)注
  • 168 瀏覽
慕課專(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)