spring给bean注入属性的三种方式

来源:互联网 发布:黑手党3知乎 编辑:程序博客网 时间:2024/05/18 20:11
  1. 一、引用方式注入:

  2. <!-- 声明集合bean-->
  3.     <util:list id="langList">
  4.         <value>c++</value>
  5.         <value>python</value>
  6.     </util:list>
  7.     <util:set id="citySet">
  8.         <value>重庆</value>
  9.         <value>天津</value>
  10.     </util:set>
  11.     <util:map id="scoreMap">
  12.         <entry key="JSD1412004" value="90"/>
  13.         <entry key="JSD1412005" value="85"/>
  14.     </util:map>
  15.     <util:properties id="paramProp">
  16.         <prop key="user">tarena</prop>
  17.         <prop key="password">123456</prop>
  18.     </util:properties>
  19.     <!-- 采用引用的方式注入集合-->
  20.     <bean id="msg2"class="com.tarena.bean.MessageBean">
  21.         <property name="langs" ref="langList"/>
  22.         <property name="cities" ref="citySet"/>
  23.         <property name="score" ref="scoreMap"/>
  24.         <property name="props" ref="paramProp"/>
  25.     </bean>  


  26. 二、  setter注入:
    1. <bean id="computer"class="com.tarena.bean.Computer">
    2.         <property name="mainboard" value="技嘉"/>
    3.         <property name="hdd" value="希捷"/>
    4.         <property name="ram" value="金士顿"/>
    5.     </bean>    
    6.     
    7.     <!-- 注入参数值-->
    8.     <bean id="msg"class="com.tarena.bean.MessageBean">
    9.         <property name="name">
    10.             <value>张三</value>
    11.         </property>
    12.         <property name="age" value="25"/>
    13.         <property name="computer" ref="computer"/>
    14.         <property name="langs">
    15.             <list>
    16.                 <value>Java</value>
    17.                 <value>php</value>
    18.                 <value>.net</value>
    19.             </list>
    20.         </property>
    21.         <property name="cities">
    22.             <set>
    23.                 <value>北京</value>
    24.                 <value>上海</value>
    25.                 <value>广州</value>
    26.             </set>
    27.         </property>
    28.         <property name="score">
    29.             <map>
    30.                 <entry key="JSD1412001" value="78"/>
    31.                 <entry key="JSD1412002" value="68"/>
    32.                 <entry key="JSD1412003" value="94"/>
    33.             </map>
    34.         </property>
    35.         <property name="props">
    36.             <props>
    37.                 <prop key="user">lhh</prop>
    38.                 <prop key="password">123456</prop>
    39.             </props>
    40.         </property>
    41.     </bean>    
    42.     
    43. </beans>


  27. 三、表达式注入
    1. <util:properties id="const" location="classpath:const.properties"/>
    2.     <!-- 注入表达式-->
    3.     <bean id="demo"class="com.tarena.bean.DemoBean">
    4.         <property name="name" value="#{msg.name}"/>
    5.         <property name="lang" value="#{msg.langs[0]}"/>
    6.         <property name="score" value="#{msg.score.JSD1412001}"/>
    7.         <property name="pageSize" value="#{const.PAGE_SIZE}"/>
    8.     </bean> 

原创粉丝点击