Spring学习笔记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默认会先初始化所有的单例的bean,因此,如果你想将有些bean特别是单例的bean的父类作为一个模板,并且给他定义了一个class,那么你一定要将这个父类bean设置成为抽象的,因为应用程序实际上会预实例化抽象bean。

使用contextSpring 2.5中引入的命名空间,可以使用专用配置元素配置属性占位符。可以在location属性中提供一个或多个位置作为逗号分隔列表。

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

FactoryBean自定义实例化逻辑

该FactoryBean接口提供了三种方法:

Object getObject():返回此工厂创建的对象的实例。实例可以共享,具体取决于此工厂是返回单例还是原型。

boolean isSingleton():单例返回true,否则false 。

Class getObjectType():返回getObject()方法返回的对象类型,不确定类型则null。

context.getBean("myBean"):返回FactoryBean实例的产品
而context.getBean("&myBean")返回FactoryBean实例本身
注意:这个bean指向的类要实现FactoryBean接口,并且重写上面写到的三个方法。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容