- 一、引用方式注入:
- <!-- 声明集合bean -->
- <util:list id="langList">
- <value>c++</value>
- <value>python</value>
- </util:list>
- <util:set id="citySet">
- <value>重庆</value>
- <value>天津</value>
- </util:set>
- <util:map id="scoreMap">
- <entry key="JSD1412004" value="90"/>
- <entry key="JSD1412005" value="85"/>
- </util:map>
- <util:properties id="paramProp">
- <prop key="user">tarena</prop>
- <prop key="password">123456</prop>
- </util:properties>
- <!-- 采用引用的方式注入集合 -->
- <bean id="msg2" class="com.tarena.bean.MessageBean">
- <property name="langs" ref="langList"/>
- <property name="cities" ref="citySet"/>
- <property name="score" ref="scoreMap"/>
- <property name="props" ref="paramProp"/>
- </bean>
- 二、 setter注入:
-
- <bean id="computer" class="com.tarena.bean.Computer">
- <property name="mainboard" value="技嘉"/>
- <property name="hdd" value="希捷"/>
- <property name="ram" value="金士顿"/>
- </bean>
- <!-- 注入参数值 -->
- <bean id="msg" class="com.tarena.bean.MessageBean">
- <property name="name">
- <value>张三</value>
- </property>
- <property name="age" value="25"/>
- <property name="computer" ref="computer"/>
- <property name="langs">
- <list>
- <value>Java</value>
- <value>php</value>
- <value>.net</value>
- </list>
- </property>
- <property name="cities">
- <set>
- <value>北京</value>
- <value>上海</value>
- <value>广州</value>
- </set>
- </property>
- <property name="score">
- <map>
- <entry key="JSD1412001" value="78"/>
- <entry key="JSD1412002" value="68"/>
- <entry key="JSD1412003" value="94"/>
- </map>
- </property>
- <property name="props">
- <props>
- <prop key="user">lhh</prop>
- <prop key="password">123456</prop>
- </props>
- </property>
- </bean>
- </beans>
- 三、表达式注入
-
- <util:properties id="const" location="classpath:const.properties"/>
- <!-- 注入表达式 -->
- <bean id="demo" class="com.tarena.bean.DemoBean">
- <property name="name" value="#{msg.name}"/>
- <property name="lang" value="#{msg.langs[0]}"/>
- <property name="score" value="#{msg.score.JSD1412001}"/>
- <property name="pageSize" value="#{const.PAGE_SIZE}"/>
- </bean>
spring给bean注入属性的三种方式
最新推荐文章于 2022-05-16 18:24:41 发布