日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PropertyPathFacoryBean获取对象的值

發布時間:2025/4/5 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PropertyPathFacoryBean获取对象的值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://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 {@AutowiredUser user;@Autowired@Qualifier("myAddress")Address add;@Autowired@Qualifier("myUser.add")Address add1;@Autowired@Qualifier("property-path")Address add2;@Testpublic 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 />這個表簽,這可能是這個特殊的作用吧

?

轉載于:https://www.cnblogs.com/SpringStudy/p/8577710.html

總結

以上是生活随笔為你收集整理的PropertyPathFacoryBean获取对象的值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。