Spring 开发之MethodInvokingFactoryBean学习

最近接触基于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)注入时有两种选择,一种是需要指定如下两个属性(也是我看到的目前所有网上资料所说):

  1. targetClass: 确定目标 class。
  2. 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 完成,但必须指定以下两个属性。

  1. targetObject: 确定目标 bean,该 bean 可以是容器中己有的 bean,也可是嵌套 bean。
  2. 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 >
                 < prop key = "site" >http://www.shitouer.cn/</ prop >
                         </ 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 实例。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值