Spring-Bean Management 笔记 BeanSpringXMLWeb
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.
- <bean id="foo" class="xx" singleton="false"/>
InitAndDestroy
<bean class="" id="foo" destroy-method="destroy" init-method="init"></bean>
- <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>
- <property name="name"><value>Foo</value></property>
- <property name="score"><value>22</value></property>
- <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>
- <property name="foo"><null/></property>
Inject dependency by constructor
<constructor-arg index="x" type="java.lang.String"></constructor-arg>
- <bean id="foo" class="xx">
- <constructor-arg index="0" type="java.lang.String>
- <value>spring</value>
- </constructor-arg>
- <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)