BeanFactory/ApplicationContext
- XmlBeanFactory
- ClassPathXmlApplicationContext
- XmlWebApplicationContext
Lazy mode
Spring default is not lazy mode, when spring starts up, it inits all beans.
If we want bean to init when calling ac.getBean()
do this:
<beans .... default-lazy-init="true">
Bean Scope
<bean scope="singleton">
singleton: return the same instance when getBean(default)
prototype: create new instance everytime when getBean
request: HTTP request life cycle
session: HTTP Session life cycle
global session: Global HTTP Session life cycle, spring portlet context
Try best to avoid scope "prototype", which is heavy memory usage and mostly unnecessary
Set property with another bean
Use <ref>
<property name="emp"> <ref bean="beanId" /> </property>
Property Injection with Collections/Array
map
set
list
collection
int[]
Array/List/Set values are primitive types
<bean id="department" class="com.gvace.collection.Department"> <property name="name" value="Business"></property> <property name="empName"> <list> <value>aaa</value> <value>bbb</value> <value>ccc</value> </list> </property> </bean>
Array/List/Set values are objects
Use ref to link the instance of other beans
<bean id="department" class="com.gvace.collection.Department"> <property name="name" value="Business"></property> <property name="employee"> <set> <ref bean="emp1"/> <ref bean="emp2"/> </set> </property> </bean> <bean id="emp1" class="com.gvace.collection.Employee"> <property name="name" value="empA" /> </bean> <bean id="emp2" class="com.gvace.collection.Employee"> <property name="name" value="empB" /> </bean>
Map property injection config
Use the following properties to build <entry>
key
key-ref
value
value-ref
<bean id="department" class="com.gvace.collection.Department"> <property name="name" value="Business"></property> <property name="employeeMap"> <map> <entry key="1" value-ref="emp1"/> <entry key="2" value-ref="emp2"/> </map> </property> </bean> <bean id="emp1" class="com.gvace.collection.Employee"> <property name="name" value="empA" /> </bean> <bean id="emp2" class="com.gvace.collection.Employee"> <property name="name" value="empB" /> </bean>
Internal Bean
<bean id="department" class="com.gvace.Department"> <property name="Manager"> <bean class="com.gvace.Manager"> <property name="name" value="managerA"/> </bean> </property> </bean>
Inherit
<!-- Graduate extends Student --> <bean id="student" class="com.gvace.impl.Student"> <property name="name" value="studentA" /> <property name="age" value="15" /> </bean> <!-- use "parent" to set the properties from extending bean --> <bean id="graduate" parent="student" class="com.gvace.impl.Graduate"> <property name="degree" value="Master" /> </bean>
java.util.Properties
Properties implements Map
So we can load the Properties like the following
<bean id="department" class="com.gvace.impl.Department"> <property name="pp"> <props> <prop key="pp1">aaa</prop> <prop key="pp2">bbb</prop> </props> </property> </bean>
NULL value
<property name="value"> <null/> </property>
Set properties by constructor
<bean id="employee" class="com.gvace.impl.Employee"> <constructor-arg index="0" type="java.lang.String" value="empA"/> <constructor-arg index="1" type="int" value="15"/> </bean>
No comments:
Post a Comment