这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果。
package com.moonlit.myspring; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Properties; public class MoonlightPoet { private String name; private int age; private Poem poem; private List<String> list; private Map<String, String> map; private Properties properties; public void perform() { System.out.println("name : " + name); System.out.println("age : " + age); poem.recite(); for (String val : list) System.out.println("in list : " + val); for (Entry<String, String> entry : map.entrySet()) System.out.println("in map : " + entry.getKey() + " -- " + entry.getValue()); for (Entry<Object, Object> entry : properties.entrySet()) System.out.println("in properties : " + entry.getKey() + " -- " + entry.getValue()); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "spring-idol.xml"); MoonlightPoet moonlightPoet = (MoonlightPoet) context.getBean("moonlightPoet"); moonlightPoet.perform(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Poem getPoem() { return poem; } public void setPoem(Poem poem) { this.poem = poem; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
该bean在xml文件中定义如下:
<bean id="moonlightPoet" class="com.moonlit.myspring.MoonlightPoet"> <property name="name" value="moonlit" /> <property name="age" value="22" /> <property name="poem" ref="sonnet29" /> <property name="list"> <list> <value>hello</value> <value>world</value> <!-- if bean, use <ref bean="XX"> --> </list> </property> <property name="map"> <map> <entry key="key1" value="value1" /> <entry key="key2" value="value2" /> <entry key="key3" value="value3" /> </map> </property> <property name="properties"> <props> <prop key="GUITAR">STRUM STRUM STRUM</prop> <prop key="CYMBAL">CRASH CRASH CRASH</prop> <prop key="HARMONICA">HUM HUM HUM</prop> </props> </property> </bean>
输出结果:
name : moonlit age : 22 When, in disgrace with fortune and men‘s eyes, I all alone beweep my outcast state, And trouble deaf heaven with my bootless cries, And look upon myself, and curse my fate, Wishing me like to one more rich in hope, Featur‘d like him, like him with friends possess‘d, Desiring this man‘s art and that man‘s scope, With what I most enjoy contented least; Yet in these thoughts myself almost despising, Haply I think on thee, and then my state, Like to the lark at break of day arising From sullen earth, sings hymns at heaven‘s gate; For thy sweet love remember‘d such wealth brings That then I scorn to change my state with kings. in list : hello in list : world in map : key1 -- value1 in map : key2 -- value2 in map : key3 -- value3 in properties : HARMONICA -- HUM HUM HUM in properties : CYMBAL -- CRASH CRASH CRASH in properties : GUITAR -- STRUM STRUM STRUM
理解:
注入简单值:
<property name="XX" value="YY" />
其中XX是变量名,YY是值。
引用其他Bean:
<property name="XX" ref="YY">
其中XX是变量名,YY是引用的bean的id。
也可以注入内部类:
<property name="XX">
<bean class="YY" />
</preperty>
其中XX是变量名,YY是内部类对应的类名。
装配List、Set和Array:
<property name="XX">
<value>YY</value>
或者
<ref bean="ZZ">
</property>
其中XX是变量名,YY是值,ZZ是引用的bean。
装配Map:
<map>
<entry key="XX" value="YY" />
或者
<entry key="XX" value-ref="YY" />
或者
<entry key-ref="XX" value="YY" />
或者
<entry key-ref="XX" value-ref="YY" />
</map>
因为map的key和value可以对应一个基础类型的值,也可以对应一个bean,所以key,value对应值,key-ref,value-ref对应bean。
装配Properties集合:
<property name="XX">
<props>
<prop key="AA">BB</prop>
....
</props>
</property>
其中AA对应key,BB对应value。
装配空值:使用<null/>元素。
使用Spring的命名空间p装配属性
我们可以在beans中添加
xmlns:p="http:www.springframework.org/schema/beans"
来使用p:作为<bean>元素所有属性的前缀来装配Bean的属性。用法如下:
<bean id="kenny" class="XX" p:song = "Jingle Bells" p:instrument-ref = "saxphone" />
-ref后缀作为一个标识来告知Spring应该装配一个引用而不是字面值。