欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本文實(shí)例講述了Spring實(shí)戰(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">
   <!--下面配置定義一個(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)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

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

相關(guān)文章

  • java方法重寫時(shí)需要注意的問題

    java方法重寫時(shí)需要注意的問題

    大家好,本篇文章主要講的是java方法重寫時(shí)需要注意的問題,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • SpringBoot對不同Bean注解的區(qū)別和使用場景說明

    SpringBoot對不同Bean注解的區(qū)別和使用場景說明

    這篇文章主要介紹了SpringBoot對不同Bean注解的區(qū)別和使用場景說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java8中CompletableFuture使用場景與實(shí)現(xiàn)原理

    Java8中CompletableFuture使用場景與實(shí)現(xiàn)原理

    CompletableFuture是java8引入的新類,該類實(shí)現(xiàn)了Future接口和 CompletionStage接口,封裝了future、forkjoin相關(guān)類來執(zhí)行異步,這篇文章主要給大家介紹了關(guān)于Java8中CompletableFuture使用場景與實(shí)現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • Java重載構(gòu)造原理與用法詳解

    Java重載構(gòu)造原理與用法詳解

    這篇文章主要介紹了Java重載構(gòu)造原理與用法,結(jié)合實(shí)例形式分析了java可變參數(shù)、方法重載、構(gòu)造器等相關(guān)概念、原理及操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • SpringCloud微服務(wù)開發(fā)基于RocketMQ實(shí)現(xiàn)分布式事務(wù)管理詳解

    SpringCloud微服務(wù)開發(fā)基于RocketMQ實(shí)現(xiàn)分布式事務(wù)管理詳解

    分布式事務(wù)是在微服務(wù)開發(fā)中經(jīng)常會(huì)遇到的一個(gè)問題,之前的文章中我們已經(jīng)實(shí)現(xiàn)了利用Seata來實(shí)現(xiàn)強(qiáng)一致性事務(wù),其實(shí)還有一種廣為人知的方案就是利用消息隊(duì)列來實(shí)現(xiàn)分布式事務(wù),保證數(shù)據(jù)的最終一致性,也就是我們常說的柔性事務(wù)
    2022-09-09
  • Java中的泛型和泛型通配符詳解

    Java中的泛型和泛型通配符詳解

    這篇文章主要介紹了Java中的泛型和泛型通配符詳解,泛型的作用就是在編譯的時(shí)候能夠檢查類型安全,并且所有的強(qiáng)制轉(zhuǎn)換都是自動(dòng)和隱式的在沒有泛型的情況的下,通過對類型Object的引用來實(shí)現(xiàn)參數(shù)的“任意化”,需要的朋友可以參考下
    2023-07-07
  • SpringBoot詳解如果通過@Value注解給靜態(tài)變量注入值

    SpringBoot詳解如果通過@Value注解給靜態(tài)變量注入值

    這篇文章主要介紹了springboot如何通過@Value給靜態(tài)變量注入值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • SpringCloud Feign原理剖析

    SpringCloud Feign原理剖析

    feign是用在微服務(wù)中,各個(gè)微服務(wù)間的調(diào)用,它是通過聲明式的方式來定義接口,而不用實(shí)現(xiàn)接口,feign讓服務(wù)間的調(diào)用變得簡單,不用各個(gè)服務(wù)去處理http client相關(guān)的邏輯,本文詳細(xì)介紹SpringCloud Feign原理,需要的朋友可以參考下
    2023-06-06
  • 淺談在eclipse中如何修改svn的用戶名和密碼

    淺談在eclipse中如何修改svn的用戶名和密碼

    這篇文章主要介紹了在eclipse中如何修改svn的用戶名和密碼的方法,在eclipse中經(jīng)常用svn進(jìn)行代碼版本控制,提交或更新代碼的時(shí)候需要我們輸入用戶名和密碼。對此感興趣的話可以來了解一下
    2020-07-07
  • MyBatis利用攔截器實(shí)現(xiàn)數(shù)據(jù)脫敏詳解

    MyBatis利用攔截器實(shí)現(xiàn)數(shù)據(jù)脫敏詳解

    現(xiàn)代網(wǎng)絡(luò)環(huán)境中,敏感數(shù)據(jù)的處理是至關(guān)重要的,敏感數(shù)據(jù)包括個(gè)人身份信息、銀行賬號、手機(jī)號碼等,所以本文主要為大家詳細(xì)介紹了MyBatis如何利用攔截器實(shí)現(xiàn)數(shù)據(jù)脫敏,希望對大家有所幫助
    2023-11-11

最新評論