最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

Bean的作用域

Bean的作用域不是singleton,容器中只有一個(gè)實(shí)例,那么如果在BeanImplOne和BeanImplTwo里個(gè)有一個(gè)BeanInterfarce的對象。那么在BeanImplOne中,對它的BeanInterfac中的屬性做更改,在BeanImplTwo里的BeanInterface實(shí)例相應(yīng)屬性的值也是會(huì)更改的么?

還是不會(huì)改變? 不會(huì)改變是什么原因,這是容器里一個(gè)Bean的實(shí)例的不同拷貝么?

正在回答

1 回答

變還是不變得看對應(yīng)的那個(gè)對象的Scope。估計(jì)你是對scope有點(diǎn)疑惑,spring中每個(gè)scope類都實(shí)現(xiàn)了org.springframework.beans.factory.config.Scope接口,然后注冊到org.springframework.beans.factory.config.CustomScopeConfigurer中。每次獲取bean的時(shí)候底層都是訪問Scope來獲取的,你也可以定義自己的Scope。比如

public?class?ThreadScope?implements?org.springframework.beans.factory.config.Scope?{
????private?final?ThreadLocal<Map<String,?Object>>?SCOPE_THREAD?=?new?ThreadLocal()?{
????????@Override
????????protected?Object?initialValue()?{
????????????return?new?HashMap<String,?Object>();
????????}
????};

????@Override
????public?Object?get(String?name,?ObjectFactory<?>?objectFactory)?{
????????if?(SCOPE_THREAD.get().containsKey(name))?{
????????????return?SCOPE_THREAD.get().get(name);
????????}
????????SCOPE_THREAD.get().put(name,?objectFactory.getObject());
????????return?SCOPE_THREAD.get().get(name);
????}

????@Override
????public?Object?remove(String?name)?{
????????return?SCOPE_THREAD.get().remove(name);
????}

????//空實(shí)現(xiàn)代表不讓spring容器管理bean
????@Override
????public?void?registerDestructionCallback(String?s,?Runnable?runnable)?{

????}

????@Override
????public?Object?resolveContextualObject(String?s)?{
????????return?null;
????}

????@Override
????public?String?getConversationId()?{
????????return?null;
????}
}

然后注冊:

<bean?class="org.springframework.beans.factory.config.CustomScopeConfigurer">
????<property?name="scopes">
????????<map>
????????????<entry?key="thread">
????????????????<bean?class="xx.xx.xx.ThreadScope"></bean>
????????????</entry>
????????</map>
????</property>
</bean>

這樣就OK了,這個(gè)Scope就是每個(gè)線程的bean都是同一個(gè)

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

Bluewind001 提問者

非常感謝!
2015-10-04 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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