Spring學(xué)習(xí)筆記3

ApplicationContext pre-instantiates all singletons by default. Therefore, it is important (at least for singleton beans) that if you have a (parent) bean definition which you intend to use only as a template, and this definition specifies a class, you must make sure to set the abstract attribute to true, otherwise the application context will actually (attempt to) pre-instantiate the abstract bean.

applicationContext默認(rèn)會(huì)先初始化所有的單例的bean,因此,如果你想將有些bean特別是單例的bean的父類作為一個(gè)模板,并且給他定義了一個(gè)class,那么你一定要將這個(gè)父類bean設(shè)置成為抽象的,因?yàn)閼?yīng)用程序?qū)嶋H上會(huì)預(yù)實(shí)例化抽象bean。

使用contextSpring 2.5中引入的命名空間,可以使用專用配置元素配置屬性占位符??梢栽趌ocation屬性中提供一個(gè)或多個(gè)位置作為逗號(hào)分隔列表。

<context:property-override location="classpath:override.properties"/>

FactoryBean自定義實(shí)例化邏輯

該FactoryBean接口提供了三種方法:

Object getObject():返回此工廠創(chuàng)建的對(duì)象的實(shí)例。實(shí)例可以共享,具體取決于此工廠是返回單例還是原型。

boolean isSingleton():?jiǎn)卫祷豻rue,否則false 。

Class getObjectType():返回getObject()方法返回的對(duì)象類型,不確定類型則null。

context.getBean("myBean"):返回FactoryBean實(shí)例的產(chǎn)品
而context.getBean("&myBean")返回FactoryBean實(shí)例本身
注意:這個(gè)bean指向的類要實(shí)現(xiàn)FactoryBean接口,并且重寫上面寫到的三個(gè)方法。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容