最近接触基于Spring的项目,发现机会每个Spring XML配置中,几乎都有MethodInvokingFactoryBean,但来出来学习下。
用来做什么
通过MethodInvokingFactory Bean类,可注入方法返回值。 MethodInvokingFactoryBean用来获得某个方法的返回值,该方法既可以是静态方法,也可以是实例方法。该方法的返回值可以注入bean实例属性,也可以直接定义成bean实例。
代码示例
获取静态方法返回值,直接定义成bean实例,XML配置
1
2
3
4
5
6
7
8
9
10
11
|
<
beans
...>
<
bean
id
=
"sysProps"
class
=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean"
>
<
property
name
=
"targetClass"
>
<
value
>java.lang.System</
value
>
</
property
>
<
property
name
=
"targetMethod"
>
<
value
>getProperties</
value
>
</
property
>
</
bean
>
</
beans
>
|
Java 执行代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package
cn.shitouer.spring.context;
import
java.util.Properties;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
Context1 {
public
static
void
main(String[] args) {
ApplicationContext ctx =
new
ClassPathXmlApplicationContext(
"cn.shitouer.springtest.xml"
);
Properties sysProps = (Properties) ctx.getBean(
"sysProps"
);
System.out.println(
"Java version is "
+ sysProps.get(
"java.version"
));
}
}
|
执行如下
1
|
Java version is 1.7.0_45
|
使用静态方法(static method)注入时有两种选择,一种是需要指定如下两个属性(也是我看到的目前所有网上资料所说):
- targetClass: 确定目标 class。
- targetMethod: 确定目标方法,确定通过目标 bean 的哪个方法返回值注入。
另一种方法是仅指定staticmethod,例如上边的Spring XML可以配置为如下可实现同样功能
1
2
3
4
5
6
7
8
|
<
beans
...>
<
bean
id
=
"sysProps"
class
=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean"
>
<
property
name
=
"staticMethod"
>
<
value
>java.lang.System.getProperties</
value
>
</
property
>
</
bean
>
</
beans
>
|
也就是说,必须Set a fully qualified static method name to invoke。
使用bean 实例的方法(instance method)返回值注入,通过 MethodInvokingFactoryBean 完成,但必须指定以下两个属性。
- targetObject: 确定目标 bean,该 bean 可以是容器中己有的 bean,也可是嵌套 bean。
- targetMethod: 确定目标方法,确定通过目标 bean 的哪个方法返回值注入。
引用如上生成的bean实例,注入site属性新生成的bean实例,XML如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<
beans
...>
<
bean
class
=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean"
>
<
property
name
=
"targetObject"
>
<
ref
local
=
"sysProps"
/>
</
property
>
<
property
name
=
"targetMethod"
>
<
value
>putAll</
value
>
</
property
>
<
property
name
=
"arguments"
>
<
props
>
</
props
>
</
property
>
</
bean
>
</
beans
>
|
Java代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package
cn.shitouer.spring.context;
import
java.util.Properties;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
Context1 {
public
static
void
main(String[] args) {
ApplicationContext ctx =
new
ClassPathXmlApplicationContext(
"cn.shitouer.springtest.xml"
);
Properties sysProps = (Properties) ctx.getBean(
"sysProps"
);
System.out.println(
"Java version is "
+ sysProps.get(
"java.version"
));
System.out.println(
"This article is Reprinted from "
+ sysProps.get(
"site"
));
}
}
|
执行结果为
1
2
|
Java version is 1.7.0_45
This article is Reprinted from http:
//www
.shitouer.cn/
|
配置文件将实例方法返回值直接定义成bean这种方式,也可用于定义静态工厂方法来创建bean 实例,或用实例工厂方法来创建 bean 实例。