欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-08-08 08:24:48|栏目:JAVA代码|点击:

本文实例讲述了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 迪杰斯特拉算法实现查找最短距离的实现

栏    目:JAVA代码

下一篇:springboot+thymeleaf+mybatis实现甘特图的详细过程

本文标题:Spring实战之获取其他Bean的属性值操作示例

本文地址:http://www.codeinn.net/misctech/165031.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有