Spring配置中的bean直接引用其它bean的属性值

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                pring配置中的bean直接引用其它bean的属性值来赋值给当前bean的属性,也可以直接调用其它bean的方法获取返回值来赋值给当前bean的属性,并且可以进行参数传递,这样可以省去在bean中注入需要获取属性值的bean。

以下是一个完整的示例:

1、需要JAVA类:

Spring配置中的bean直接引用其它bean的属性值package com.service.test;public class Bean1 int v1 = 1public int getV1() {  return v1; } public void setV1(int v1) {  this.v1 = v1; } public int getTestValue(int v1) {  return v1; } public String getTestValue2(String str) {  return str; } }package com.service.test;public class Bean2 int v1; int v2; int v3; String v4Str; public int getV1() {  return v1; } public void setV1(int v1) {  this.v1 = v1; } public int getV2() {  return v2; } public void setV2(int v2) {  this.v2 = v2; }  public int getV3() {  return v3; } public void setV3(int v3) {  this.v3 = v3; } public String getV4Str() {  return v4Str; } public void setV4Str(String str) {  v4Str = str; } }package com.service.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test /**  * @param args  */ public static void main(String[] args) {  // TODO Auto-generated method stub  ApplicationContext context = new ClassPathXmlApplicationContext("testBean.xml");  Bean2 bean2 = (Bean2)context.getBean("bean2");  System.out.println(bean2.getV1());  System.out.println(bean2.getV2());  System.out.println(bean2.getV3());  System.out.println(bean2.getV4Str()); }}

2、需要XML配置:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">    <bean id="bean1" class="com.service.test.Bean1"> </bean> <bean id="bean2" class="com.service.test.Bean2">  <property name="v1" value="#{bean1.v1}"/>  <property name="v2" value="#{bean1.getV1()}"/>  <property name="v3" value="#{bean1.getTestValue(3)}"/>  <property name="v4Str" value="#{'Hello '+bean1.getTestValue2('baby')}"/> </bean></beans>



           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值