spring的注入方式

在spring对类进行注入时主要有通过属性,构造函数,以及自动注入方式,还可在beans根标签中使用default-autowire="byName|byType|..."来对beans中的所有bean进行自动注入

  1. 通过属性注入使用<propety id="xxx" ref="">的方式
  2. 通过构造方法使用<constructor-arg ref="xxx">如果构造方法中有多个参数则依次使用此标签<constructor-arg ref="arg1"><constructor-arg ref="arg2">
  3. 自动注入在<bean id="xx" class="xxx.xx" autowire="xx">xx可为byName,byType.default,no,constructor使用byType时,注意不应用两个相同类型的类,否则会抛异常。

可使用

  <bean id="xx" class="xxx.xx">
        <property name="xx" value="xx"></property>
        <property name="xx" value="xx"></property>
        <property name="xx" value="xx"></property>
    </bean>

来注入一个对象property中的name为id为xx对象的属性名,value则为属性值

也可注入集合

<propety name="xx">

<list>

<value>xx</value>

<value>xx</value>

<ref></ref>

</list>

</property>

posted on 2013-04-26 22:18  zcjava  阅读(122)  评论(0)    收藏  举报