溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring中怎么獲取其他Bean的屬性值

發(fā)布時(shí)間:2021-07-26 14:15:11 來源:億速云 閱讀:170 作者:Leah 欄目:編程語言

Spring中怎么獲取其他Bean的屬性值,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

一 配置

<?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">   <!--下面配置定義一個(gè)將要被引用的目標(biāo)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實(shí)例的getter方法返回值定義成son1 Bean -->   <bean id="son1" class=      "org.springframework.beans.factory.config.PropertyPathFactoryBean">      <!-- 確定目標(biāo)Bean,指定son1 Bean來自哪個(gè)Bean的getter方法 -->      <property name="targetBeanName" value="person"/>      <!-- 指定son1 Bean來自目標(biāo)bean的哪個(gè)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實(shí)例 -->   <bean id="theAge" class=      "org.springframework.beans.factory.config.PropertyPathFactoryBean">      <!-- 確定目標(biāo)Bean,表明theAge Bean來自哪個(gè)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實(shí)例 -->   <bean id="theAge2" class=      "org.springframework.beans.factory.config.PropertyPathFactoryBean">      <!-- 確定目標(biāo)Bean,表明theAge2 Bean來自哪個(gè)Bean的屬性。        此處采用嵌套Bean定義目標(biāo)Bean -->      <property name="targetObject">        <!-- 目標(biāo)Bean不是容器中已經(jīng)存在的Bean, 而是如下的嵌套Bean-->        <bean class="org.crazyit.app.service.Person">           <property name="age" value="30"/>        </bean>      </property>      <!-- 指定theAge2 Bean來自目標(biāo)bean的哪個(gè)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)于Spring中怎么獲取其他Bean的屬性值問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI