1.我们有时候想获取一个bean 的一个属性的值,那我们该如何操作???
2.使用Spring为我们提供的PropertyPathFactoryBean类来获取
(1)有如下实体类
User:
package SpringPropertyPathFactoryBean.propertyPathFactoryBean.entity; public class User { private String name; private Integer id; private Address add; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Address getAdd() { return add; } public void setAdd(Address add) { this.add = add; } public String toString() { return "User [name=" + name + ", id=" + id + ", add=" + add + "]"; } public User() { } public User(String name, Integer id, Address add) { this.name = name; this.id = id; this.add = add; } }
Address:
package SpringPropertyPathFactoryBean.propertyPathFactoryBean.entity; public class Address { private String city ; private String area; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public Address(String city, String area) { this.city = city; this.area = area; } public Address() { } public String toString() { return "Address [city=" + city + ", area=" + area + "]"; } }
配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <bean class="SpringPropertyPathFactoryBean.propertyPathFactoryBean.entity.User" id="myUser"> <property name="name" value="张三"></property> <property name="id" value="19"></property> <property name="add" > <bean class="SpringPropertyPathFactoryBean.propertyPathFactoryBean.entity.Address"> <property name="area" value="孝南区"></property> <property name="city" value="孝感"></property> </bean> </property> </bean> <bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean" name="myAddress"> <!-- 获取属性的名字--> <property name="propertyPath" value="add"></property> <!-- 目标Bean的名字--> <property name="targetBeanName" value="myUser"></property> </bean> <!-- 书上是这样写的,不知道为什么出错,难道是版本问题 --> <bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean" name="myAddress1"> <!-- 获取属性的名字--> <property name="propertyPath" value="add"></property> <!-- 目标对象 --> <property name="targetObject" ref="myUser"></property> </bean> <!-- 通过名字来来简化配置 --> <bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean" name="myUser.add" /> <!-- 通过特殊的标签来配置 --> <util:property-path path="myUser.add" id="property-path"/> </beans>
Test类如下:
package SpringPropertyPathFactoryBean.propertyPathFactoryBean; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import SpringPropertyPathFactoryBean.propertyPathFactoryBean.entity.Address; import SpringPropertyPathFactoryBean.propertyPathFactoryBean.entity.User; @RunWith(value=SpringJUnit4ClassRunner.class) @ContextConfiguration(value="classpath:applicationContext.xml") public class PropertyAciquereTest { @Autowired User user; @Autowired @Qualifier("myAddress") Address add; @Autowired @Qualifier("myUser.add") Address add1; @Autowired @Qualifier("property-path") Address add2; @Test public void test() throws Exception { System.out.println(user); System.out.println(add); System.out.println(add2); } }
结果:
推荐使用 <util:property-path path="myUser.add" id="property-path"/>这中方式配置获取值
默认使用path作为bean的name,配置id后使用id的值作为bean的名字
注意:这里我是使用Spring-test测试的,我没有使用扫描包即:<context:component-scan />这个表签,这可能是这个特殊的作用吧