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

  1. 一、引用方式注入:

  2.     <util:list id="langList">
  3.         <value>c++value>
  4.         <value>pythonvalue>
  5.     util:list>
  6.     <util:set id="citySet">
  7.         <value>重庆value>
  8.         <value>天津value>
  9.     util:set>
  10.     <util:map id="scoreMap">
  11.         <entry key="JSD1412004" value="90"/>
  12.         <entry key="JSD1412005" value="85"/>
  13.     util:map>
  14.     <util:properties id="paramProp">
  15.         <prop key="user">tarenaprop>
  16.         <prop key="password">123456prop>
  17.     util:properties>
  18.     
  19.     <bean id="msg2" class="com.tarena.bean.MessageBean">
  20.         <property name="langs" ref="langList"/>
  21.         <property name="cities" ref="citySet"/>
  22.         <property name="score" ref="scoreMap"/>
  23.         <property name="props" ref="paramProp"/>
  24.     bean>  


  25. 二、  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>Javavalue>
    17.                 <value>phpvalue>
    18.                 <value>.netvalue>
    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">lhhprop>
    38.                 <prop key="password">123456prop>
    39.             props>
    40.         property>
    41.     bean>    
    42.     
    43. beans>


  26. 三、表达式注入
    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> 

你可能感兴趣的:(软件开发)