目录
bean的创建顺序
在 spring 的 ioc 容器, 默认是按照xml文件里配置的从上到下的顺序创建 bean 对象
bean 对象的单例和多例
在 spring 的 ioc 容器,在默认是按照单例创建的,即配置一个 bean 对象后,ioc 容器只会创建一个 bean 实例。
如果,我们希望 ioc 容器配置的某个 bean 对象,是以多个实例形式创建的则可以通过配置scope="prototype" 来指定。
<bean name="student04" scope="prototype" class="com.asl.spring.beans.Student"/>
通过属性配置Bean
<bean class="com.asl.spring.Student" id="student01">
<property name="name" value="jack01"/>
<property name="sex" value="man"/>
<property name="age" value="18"/>
</bean>
通过构造器配置Bean
<bean class="com.asl.spring.Student" id="student03">
<constructor-arg value="jack03" index="0" type="java.lang.String"/>
<constructor-arg value="man" index="1" type="java.lang.String"/>
<constructor-arg value="18" index="2" type="java.lang.String"/>
</bean>
通过 p 名称空间配置 Bean
<bean class="com.asl.spring.Student" id="student04"
p:name="jack04"
p:sex="man"
p:age="14"
/>
通过 ref 注入其他Bean
<bean class="类路径" id="id01"/>
<bean class="类路径" id="id02">
<property name="属性名" ref="id01"/>
</bean>
配置内部Bean
<bean class="" id=""/>
<bean class="" id="">
<property name="">
<bean class=""/>
</property>
</bean>
引用/注入集合/数组类型
<!--配置list属性-->
<property name="">
<list>
<ref bean=""/>
<ref bean=""/>
<ref bean=""/>
</list>
</property>
<!--配置map属性-->
<property name="">
<map>
<entry key="" value-ref=""/>
<entry key="" value-ref=""/>
</map>
</property>
<!--配置set属性-->
<property name="">
<set>
<ref bean=""/>
<ref bean=""/>
<ref bean=""/>
</set>
</property>
<!--配置数组-->
<property name="">
<array>
<value>value1</value>
<value>value2</value>
<value>value3</value>
<value>value4</value>
</array>
</property>
<!--配置properties-->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
级联属性赋值
<bean class="" id="a01"/>
<bean class="" id="a02">
<property name="a01" ref="a01"/>
<property name="a01.name" value=""/>
</bean>
bean 配置信息重用(继承)
<!--如果配置了abstract属性,那么就不能被实例化,只能被用于继承-->
<!-- 当我们把某个bean设置为 abstract="true" 这个bean只能被继承,而不能实例化了 -->
<bean class="com.asl.spring.beans.Student" id="student11" >
<property name="name" value="jack"/>
<property name="sex" value="man"/>
<property name="age" value="18"/>
</bean>
<!-- parent="student12" 就是继承使用了 student11 的配置信息 -->
<bean class="com.asl.spring.beans.Student" id="student12" parent="student11"/>