Spring bean的配置

创建对象

给对象赋值

    由spring完成


配置bean

<bean id="" class="">

<property name="" value="">

id:用来表示bean,就是bean的名字

property 属性注入 setter方法注入

constructor-arg 构造器注入 (需要类中有构造器) 

可以指定参数位置或者类型区分重载的构造器!(type、index)

如果包含特殊字符,可以使用<![CDATA[   ]]>


使用property的ref属性建立bean之间的关系

集合配置方法:

LIST:

<property name="cars">
    <list>
        <ref bean="car1">
        <ref bean="car2"> 
    </list>

MAP:

<property name="cars">
  <map>
    <entry key="AA" value-ref="car"> </entty>
  </map>

Properties:

<bean id="dataSource" class="">
  <property name="properties">
    <props>
      <prop key="user">root</prop>
      <prop key="password">root</prop> 
      <prop key="jdbcUrl">jdbc:mysql:///test</prop>

配置独立的集合bean供多个bean进行引用(需要导入util命名空间)

<util:list id="cars">
  <ref bean="car1"/>
  <ref bean="car2"/>
</util:list>
<!-- 引用 -->
<bean id="person" class="">
  <property name="name" value="li"></..>
  </property name="cars" ref="cars"></.>

通过p命名空间为bean的属性赋值,需要导入p命名空间

<bean id="person" class="" p:name="li" p:cars-ref="cars"></bean>

applicationContext来初始化IOC容器

猜你喜欢

转载自blog.csdn.net/lky888666/article/details/80529327