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

  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> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值