如何接收配置的bean的返回值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何接收配置的bean的返回值相关的知识,希望对你有一定的参考价值。

参考技术A   pring配置中的bean直接引用其它bean的属性值来赋值给当前bean的属性,也可以直接调用其它bean的方法获取返回值来赋值给当前bean的属性,并且可以进行参数传递,这样可以省去在bean中注入需要获取属性值的bean。
  以下是一个完整的示例:
  1、需要JAVA类:

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

package com.service.test;
public class Bean1
int v1 = 1;
public 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>本回答被提问者和网友采纳

linux shell中如何接收程序返回值

参考技术A $?
$?就是表示上一次脚本或者命令退出时的返回值。通常,0代表成功;非0代表出现错误。
类似的一些符号如$0 $1 $2 $@ $# 。

Before we move on, there is a perversity about tests in Bash shells that I want to discuss. It turns out, because of a historical accident that now might as well be cast in concrete, when a test is conducted or a command returns a result value, the numerical value for "true" is 0, and "false" is 1. Those of you who have some programming experience will likely find this reversal of intuition as annoying as I do.

Here is a way to get the result of the most recent logical test (and to show the weird reversal described above):

$ test -e .
$ echo $?
0

$ test -e xyz
$ echo $?
1本回答被提问者和网友采纳

以上是关于如何接收配置的bean的返回值的主要内容,如果未能解决你的问题,请参考以下文章

如何接收线程池执行后的返回值

idea里面的java代码怎么自动获取返回值

linux shell中如何接收程序返回值

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

函数的返回值是如何带出和接收的以及内存中的活动情况.RP

JS如何获取值