<!-- target bean to be referenced by name --><beanid="testBean"class="org.springframework.beans.TestBean"scope="prototype"> <propertyname="age"value="10"/> <propertyname="spouse"> <beanclass="org.springframework.beans.TestBean"> <propertyname="age"value="11"/> </bean> </property></bean><!-- results in 10, which is the value of property 'age' of bean 'testBean' --><beanid="testBean.age"class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
<!-- target bean to be referenced by name --><beanid="testBean"class="org.springframework.beans.TestBean"scope="prototype"> <propertyname="age"value="10"/> <propertyname="spouse"> <beanclass="org.springframework.beans.TestBean"> <propertyname="age"value="11"/> </bean> </property></bean><!-- results in 10, which is the value of property 'age' of bean 'testBean' --><util:property-pathid="name"path="testBean.age"/>
<!-- target bean to be referenced by name --><beanid="person"class="org.springframework.beans.TestBean"scope="prototype"> <propertyname="age"value="10"/> <propertyname="spouse"> <beanclass="org.springframework.beans.TestBean"> <propertyname="age"value="11"/> </bean> </property></bean><!-- results in 11, which is the value of property 'spouse.age' of bean 'person' --><beanid="theAge"class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <propertyname="targetBeanName"value="person"/> <propertyname="propertyPath"value="spouse.age"/></bean>
在以下示例中,针对内部 bean 评估路径:
<!-- results in 12, which is the value of property 'age' of the inner bean --><beanid="theAge"class="org.springframework.beans.factory.config.PropertyPathFactoryBean"> <propertyname="targetObject"> <beanclass="org.springframework.beans.TestBean"> <propertyname="age"value="12"/> </bean> </property> <propertyname="propertyPath"value="age"/></bean>
还有一种快捷方式,其中 bean 名称是属性路径。以下示例显示了快捷方式:
<!-- results in 10, which is the value of property 'age' of bean 'person' --><beanid="person.age"class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
<!-- creates a java.util.Properties instance with values loaded from the supplied location --><beanid="jdbcConfiguration"class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <propertyname="location"value="classpath:com/foo/jdbc-production.properties"/></bean>
<!-- creates a java.util.Properties instance with values loaded from the supplied location --><util:propertiesid="jdbcConfiguration"location="classpath:com/foo/jdbc-production.properties"/>
使用<util:list/>
考虑以下示例:
<!-- creates a java.util.List instance with values loaded from the supplied 'sourceList' --><beanid="emails"class="org.springframework.beans.factory.config.ListFactoryBean"> <propertyname="sourceList"> <list> <value>pechorin@hero.org</value> <value>raskolnikov@slums.org</value> <value>stavrogin@gov.org</value> <value>porfiry@gov.org</value> </list> </property></bean>
<!-- creates a java.util.List instance with the supplied values --><util:listid="emails"> <value>pechorin@hero.org</value> <value>raskolnikov@slums.org</value> <value>stavrogin@gov.org</value> <value>porfiry@gov.org</value></util:list>
您还可以使用<util:list/> 元素上的 list-class 属性显式控制实例化和填充的 List 的确切类型。例如,如果我们确实需要实例化 java.util.LinkedList,我们可以使用以下配置:
<!-- creates a java.util.Map instance with the supplied key-value pairs --><util:mapid="emails"> <entrykey="pechorin"value="pechorin@hero.org"/> <entrykey="raskolnikov"value="raskolnikov@slums.org"/> <entrykey="stavrogin"value="stavrogin@gov.org"/> <entrykey="porfiry"value="porfiry@gov.org"/></util:map>
<!-- creates a java.util.Set instance with the supplied values --><util:setid="emails"> <value>pechorin@hero.org</value> <value>raskolnikov@slums.org</value> <value>stavrogin@gov.org</value> <value>porfiry@gov.org</value></util:set>