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程序设计有所帮助。

相关文章

  • Java中SimpleDateFormat用法详解

    Java中SimpleDateFormat用法详解

    SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化.这篇文章主要介绍了Java中SimpleDateFormat用法详解,需要的朋友可以参考下
    2017-03-03
  • 手把手带你实现第一个Mybatis程序

    手把手带你实现第一个Mybatis程序

    这篇文章主要介绍了mybatis实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-07-07
  • Springboot错误处理机制实现原理解析

    Springboot错误处理机制实现原理解析

    这篇文章主要介绍了springboot错误处理机制实现原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 浅谈SpringCloud之Ribbon详解

    浅谈SpringCloud之Ribbon详解

    这篇文章主要介绍了浅谈SpringCloud之Ribbon,文中有非常详细的代码示例,对正在学习SpringCloud的小伙伴们有很大的帮助,需要的朋友可以参考下
    2021-05-05
  • mybatisplus的坑 insert标签insert into select无参数问题的解决

    mybatisplus的坑 insert标签insert into select无参数问题的解决

    这篇文章主要介绍了mybatisplus的坑 insert标签insert into select无参数问题的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 如何设计一个秒杀系统

    如何设计一个秒杀系统

    本文主要介绍了如何设计一个秒杀系统的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • 关于pom.xml中maven无法下载springcloud包问题

    关于pom.xml中maven无法下载springcloud包问题

    小编遇到这样一个问题spring-cloud-starter-feign,spring-cloud-starter-eureka 一直无法下载,maven仓库中包路径显示为unknown,怎么解决呢?下面小编给大家带来了pom.xml中maven无法下载springcloud包问题,需要的朋友可以参考下
    2022-08-08
  • Java通过SSM完成水果商城批发平台流程

    Java通过SSM完成水果商城批发平台流程

    这是一个使用了java+SSM开发的网上水果商城批发平台,是一个实战小练习,具有水果商城批发该有的所有功能,感兴趣的朋友快来看看吧
    2022-06-06
  • Spring项目中使用Junit单元测试并配置数据源的操作

    Spring项目中使用Junit单元测试并配置数据源的操作

    这篇文章主要介绍了Spring项目中使用Junit单元测试并配置数据源的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • java 枚举enum的用法(与在switch中的用法)

    java 枚举enum的用法(与在switch中的用法)

    这篇文章主要介绍了java 枚举enum的用法(与在switch中的用法),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10

最新评论