最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring實戰(zhàn)之獲取其他Bean的屬性值操作示例

 更新時間:2019年12月02日 08:44:36   作者:cakincqm  
這篇文章主要介紹了Spring實戰(zhàn)之獲取其他Bean的屬性值操作,結(jié)合實例形式分析了Spring操作Bean屬性值的相關(guān)配置與實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了Spring實戰(zhàn)之獲取其他Bean的屬性值操作。分享給大家供大家參考,具體如下:

一 配置

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util-4.0.xsd">
   <!--下面配置定義一個將要被引用的目標bean-->
   <bean id="person" class="org.crazyit.app.service.Person">
      <property name="age" value="30"/>
      <property name="son">
        <!-- 使用嵌套Bean定義setSon()方法的參數(shù)值 -->
        <bean class="org.crazyit.app.service.Son">
           <property name="age" value="11" />
        </bean>
      </property>
   </bean>
   <!-- 將指定Bean實例的getter方法返回值定義成son1 Bean -->
   <bean id="son1" class=
      "org.springframework.beans.factory.config.PropertyPathFactoryBean">
      <!-- 確定目標Bean,指定son1 Bean來自哪個Bean的getter方法 -->
      <property name="targetBeanName" value="person"/>
      <!-- 指定son1 Bean來自目標bean的哪個getter方法,son代表getSon() -->
      <property name="propertyPath" value="son"/>
   </bean>
   <!-- 簡化配置
   <util:property-path id="son1" path="person.son"/> -->
   <!-- 下面定義son2 Bean -->
   <bean id="son2" class="org.crazyit.app.service.Son">
      <property name="age">
        <!-- 使用嵌套Bean為調(diào)用setAge()方法指定參數(shù)值 -->
        <!-- 以下是訪問指定Bean的getter方法的簡單方式,
        person.son.age代表獲取person.getSon().getAge()-->
        <bean id="person.son.age" class=
           "org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
      </property>
   </bean>
   <!-- 簡化配置
   <bean id="son2" class="org.crazyit.app.service.Son">
      <property name="age">
        <util:property-path path="person.son.age"/>
      </property>
   </bean> -->
   <!-- 將基本數(shù)據(jù)類型的屬性值定義成Bean實例 -->
   <bean id="theAge" class=
      "org.springframework.beans.factory.config.PropertyPathFactoryBean">
      <!-- 確定目標Bean,表明theAge Bean來自哪個Bean的getter方法的返回值 -->
      <property name="targetBeanName" value="person"/>
      <!-- 使用復(fù)合屬性來指定getter方法。son.age代表getSon().getAge() -->
      <property name="propertyPath" value="son.age"/>
   </bean>
   <!-- 簡化配置
   <util:property-path id="theAge" path="person.son.age"/> -->
   <!-- 將基本數(shù)據(jù)類型的屬性值定義成Bean實例 -->
   <bean id="theAge2" class=
      "org.springframework.beans.factory.config.PropertyPathFactoryBean">
      <!-- 確定目標Bean,表明theAge2 Bean來自哪個Bean的屬性。
        此處采用嵌套Bean定義目標Bean -->
      <property name="targetObject">
        <!-- 目標Bean不是容器中已經(jīng)存在的Bean, 而是如下的嵌套Bean-->
        <bean class="org.crazyit.app.service.Person">
           <property name="age" value="30"/>
        </bean>
      </property>
      <!-- 指定theAge2 Bean來自目標bean的哪個getter方法,age代表getAge() -->
      <property name="propertyPath" value="age"/>
   </bean>
</beans>

二 Bean

1 Person

package org.crazyit.app.service;
public class Person
{
   private int age;
   private Son son;
   // age的setter和getter方法
   public void setAge(int age)
   {
      this.age = age;
   }
   public int getAge()
   {
      return this.age;
   }
   // son的setter和getter方法
   public void setSon(Son son)
   {
      this.son = son;
   }
   public Son getSon()
   {
      return this.son;
   }
}

2 Son

package org.crazyit.app.service;
public class Son
{
   private int age;
   // age的setter和getter方法
   public void setAge(int age)
   {
      this.age = age;
   }
   public int getAge()
   {
      return this.age;
   }
   public String toString()
   {
      return "Son[age=" + age + "]";
   }
}

三 測試類

package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class SpringTest
{
  public static void main(String[] args)
  {
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    System.out.println("系統(tǒng)獲取的son1:"
      + ctx.getBean("son1"));
    System.out.println("系統(tǒng)獲取son2:"
      + ctx.getBean("son2"));
    System.out.println("系統(tǒng)獲取theAge的值:"
      + ctx.getBean("theAge"));
    System.out.println("系統(tǒng)獲取theAge2的值:"
      + ctx.getBean("theAge2"));
  }
}

四 測試結(jié)果

系統(tǒng)獲取的son1:Son[age=11]
系統(tǒng)獲取son2:Son[age=11]
系統(tǒng)獲取theAge的值:11
系統(tǒng)獲取theAge2的值:30

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • Sentinel自定義異常的三種實現(xiàn)方式

    Sentinel自定義異常的三種實現(xiàn)方式

    Spring Cloud Alibaba Sentinel 是目前主流并開源的流量控制和系統(tǒng)保護組件,Spring Cloud Alibaba Sentinel 有 3 種自定義異常的實現(xiàn)方式,本文小編將通過代碼示例給大家詳細的介紹這三種實現(xiàn)方式,需要的朋友可以參考下
    2023-11-11
  • Spring?Security自定義失敗處理器問題

    Spring?Security自定義失敗處理器問題

    這篇文章主要介紹了Spring?Security自定義失敗處理器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 詳解java設(shè)計模式中的門面模式

    詳解java設(shè)計模式中的門面模式

    門面模式又叫外觀模式(Facade?Pattern),主要用于隱藏系統(tǒng)的復(fù)雜性,并向客戶端提供了一個客戶端可以訪問系統(tǒng)的接口,本文通過實例代碼給大家介紹下java門面模式的相關(guān)知識,感興趣的朋友一起看看吧
    2022-02-02
  • java?使用readLine()?亂碼的解決

    java?使用readLine()?亂碼的解決

    這篇文章主要介紹了java使用readLine()亂碼的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java八種基本變量作為類的成員變量的默認值操作

    Java八種基本變量作為類的成員變量的默認值操作

    這篇文章主要介紹了Java八種基本變量作為類的成員變量的默認值操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java關(guān)鍵字this(動力節(jié)點Java學(xué)院整理)

    Java關(guān)鍵字this(動力節(jié)點Java學(xué)院整理)

    java中的this隨處可見,用法也多。通常情況下理解this關(guān)鍵字還是很容易的,但是在我初學(xué)的時候,有一個疑問卻一直不能很清晰的理解,現(xiàn)在慢慢的理解了,下面通過本文給大家記錄下,有需要的朋友參考下
    2017-03-03
  • Docker 存儲驅(qū)動詳細介紹

    Docker 存儲驅(qū)動詳細介紹

    這篇文章主要介紹了Docker 存儲驅(qū)動詳細介紹的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • Java加密解密和數(shù)字簽名完整代碼示例

    Java加密解密和數(shù)字簽名完整代碼示例

    這篇文章主要介紹了Java加密解密和數(shù)字簽名完整代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-12-12
  • 詳解Java如何優(yōu)雅的實現(xiàn)異常捕獲

    詳解Java如何優(yōu)雅的實現(xiàn)異常捕獲

    在一個優(yōu)秀的項目中一定少不了對程序流程良好的異常捕獲與日志打印,所以本文主要為大家介紹了如何優(yōu)雅的實現(xiàn)異常捕獲與日志打印輸出,有需要的可以參考下
    2023-09-09
  • Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決

    Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決

    這篇文章主要介紹了Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01

最新評論

会东县| 喀喇| 九江市| 蚌埠市| 张家界市| 东港市| 平江县| 千阳县| 昭平县| 石家庄市| 隆昌县| 花莲市| 秀山| 克什克腾旗| 巴里| 宁津县| 怀远县| 进贤县| 兴义市| 自治县| 陆丰市| 正定县| 临海市| 武鸣县| 石阡县| 伽师县| 南投县| 调兵山市| 六安市| 始兴县| 河源市| 溧阳市| 长汀县| 九台市| 丹江口市| 布尔津县| 菏泽市| 岳西县| 长白| 岳普湖县| 武功县|