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è)方法。