Spring-Bean Management 笔记

BeanFactory vs ApplicationContext
Both of them are Interface
ApplicationContext extends BeanFactory indirectly.
(api for ApplicationContext:http://www.springframework.org/docs/api/org/springframework/context/ApplicationContext.html)

ApplicationContext provide extra functionality such as I18N, send event to the bean which subscribed as listener.
provide common method for loading resource.

Three implementation for AppilcationContext is widely used.
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
XmlWebApplicationContext(load context information in Web System)

BeanFactory lazy load all beans, it is actually loaded when getBean() is invoked.
ApplicationContext preload all the singleton Bean.

Lift cycle of a Bean in spring. 
1.instantiation
2.configurate all the propeties defined in the Xml file.
3(optional).invoke setBeanName(beanId) if the bean implements the BeanNameAware
4(optional).inovke setBeanFacoty() if the bean implements the BeanFactoryAware
5(optional for ApplicationContext). invoke setApplicationContext(), if implements ApplicationContextAware
6(optional). If bean associated with any BeanPostProcessor, postProcessBeforeInitialzation() invoked.
7(optional). If init-method was indicated, it get inovked.
8(optional).If bean associated with any BeanPostProcessor, postProcessAfterInitialzation() invoked.

Basic Load
The singleton is the default mode in the context.
following is the way to turn off this mode.
<bean class="com.cwl.IhateSingleton" id="noSingleton" singleton="false"></bean>Spring will automatically convert value to the correspondent type of the property defiend in the class with reflection.
object  member (refer other bean)

BeanFacotoryPostProcessor
Spring built-in implementation:PropertyPlaceholderConfigurer,CustomEditorConfigurer

I18N support
ResourceBundleMessageSource

lister event
In the life cycle of the bean, ApplicationContext will send lots of event, deails refer P75 spring in action.

xml 代码
  1. <bean id="foo" class="xx" singleton="false"/>  


InitAndDestroy
<bean class="" id="foo" destroy-method="destroy" init-method="init"></bean>

xml 代码
  1. <bean id="foo" class="xx" init-method="setup" destroy-method="destory"/>  


Note: the parenthesis was not included in the above configuration.

Inject dependency by Set method
primitive member
<property name="name"><value></value></property> <property name="score"></property>

xml 代码
  1. <property name="name"><value>Foo</value></property>  
  2. <property name="score"><value>22</value></property>  

 

xml 代码
  1. <property name="bar"><ref bean="bar"/></property>  


load list,set,map P54
Note: there is a limitation when loading map
The key should is confined with "String" type.

How to set Null value.
<property name="foo"><null></null></property>

xml 代码
  1. <property name="foo"><null/></property>  



Inject dependency by constructor
<constructor-arg index="x" type="java.lang.String"></constructor-arg>

xml 代码
  1. <bean id="foo" class="xx">  
  2.   <constructor-arg index="0" type="java.lang.String>  
  3.     <value>spring</value>  
  4.   </constructor-arg>  
  5. <bean>  


Automatically Loading
byName, byType, constructor,autodetect P61

Specially Bean in Spring
BeanPostProcessor
procedure
1.implement  BeanPostProcessor
2.registor bean in xml as other bean(Spring will check the bean if implement BeanPostProcessor on the fly)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值