Spring实战之获取其他Bean的属性值操作示例

 更新时间:2019年12月02日 08:44:36   作者:cakincqm  
这篇文章主要介绍了Spring实战之获取其他Bean的属性值操作,结合实例形式分析了Spring操作Bean属性值的相关配置与实现技巧,需要的朋友可以参考下

本文实例讲述了Spring实战之获取其他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()方法的参数值 -->
        <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为调用setAge()方法指定参数值 -->
        <!-- 以下是访问指定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> -->
   <!-- 将基本数据类型的属性值定义成Bean实例 -->
   <bean id="theAge" class=
      "org.springframework.beans.factory.config.PropertyPathFactoryBean">
      <!-- 确定目标Bean,表明theAge Bean来自哪个Bean的getter方法的返回值 -->
      <property name="targetBeanName" value="person"/>
      <!-- 使用复合属性来指定getter方法。son.age代表getSon().getAge() -->
      <property name="propertyPath" value="son.age"/>
   </bean>
   <!-- 简化配置
   <util:property-path id="theAge" path="person.son.age"/> -->
   <!-- 将基本数据类型的属性值定义成Bean实例 -->
   <bean id="theAge2" class=
      "org.springframework.beans.factory.config.PropertyPathFactoryBean">
      <!-- 确定目标Bean,表明theAge2 Bean来自哪个Bean的属性。
        此处采用嵌套Bean定义目标Bean -->
      <property name="targetObject">
        <!-- 目标Bean不是容器中已经存在的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("系统获取的son1:"
      + ctx.getBean("son1"));
    System.out.println("系统获取son2:"
      + ctx.getBean("son2"));
    System.out.println("系统获取theAge的值:"
      + ctx.getBean("theAge"));
    System.out.println("系统获取theAge2的值:"
      + ctx.getBean("theAge2"));
  }
}

四 测试结果

系统获取的son1:Son[age=11]
系统获取son2:Son[age=11]
系统获取theAge的值:11
系统获取theAge2的值:30

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

相关文章

  • Sentinel自定义异常的三种实现方式

    Sentinel自定义异常的三种实现方式

    Spring Cloud Alibaba Sentinel 是目前主流并开源的流量控制和系统保护组件,Spring Cloud Alibaba Sentinel 有 3 种自定义异常的实现方式,本文小编将通过代码示例给大家详细的介绍这三种实现方式,需要的朋友可以参考下
    2023-11-11
  • Spring Security自定义失败处理器问题

    Spring Security自定义失败处理器问题

    这篇文章主要介绍了Spring Security自定义失败处理器问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • 详解java设计模式中的门面模式

    详解java设计模式中的门面模式

    门面模式又叫外观模式(Facade Pattern),主要用于隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口,本文通过实例代码给大家介绍下java门面模式的相关知识,感兴趣的朋友一起看看吧
    2022-02-02
  • java 使用readLine() 乱码的解决

    java 使用readLine() 乱码的解决

    这篇文章主要介绍了java使用readLine()乱码的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • Java八种基本变量作为类的成员变量的默认值操作

    Java八种基本变量作为类的成员变量的默认值操作

    这篇文章主要介绍了Java八种基本变量作为类的成员变量的默认值操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java关键字this(动力节点Java学院整理)

    Java关键字this(动力节点Java学院整理)

    java中的this随处可见,用法也多。通常情况下理解this关键字还是很容易的,但是在我初学的时候,有一个疑问却一直不能很清晰的理解,现在慢慢的理解了,下面通过本文给大家记录下,有需要的朋友参考下
    2017-03-03
  • Docker 存储驱动详细介绍

    Docker 存储驱动详细介绍

    这篇文章主要介绍了Docker 存储驱动详细介绍的相关资料,需要的朋友可以参考下
    2016-10-10
  • Java加密解密和数字签名完整代码示例

    Java加密解密和数字签名完整代码示例

    这篇文章主要介绍了Java加密解密和数字签名完整代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-12-12
  • 详解Java如何优雅的实现异常捕获

    详解Java如何优雅的实现异常捕获

    在一个优秀的项目中一定少不了对程序流程良好的异常捕获与日志打印,所以本文主要为大家介绍了如何优雅的实现异常捕获与日志打印输出,有需要的可以参考下
    2023-09-09
  • Spring多数据源导致配置失效的解决

    Spring多数据源导致配置失效的解决

    这篇文章主要介绍了Spring多数据源导致配置失效的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01

最新评论