Spring实战之org.springframework.beans.factory.config.MethodInvokingFactoryBean

 

在用spring管理我们的类的时候有时候希望有些属性值是来源于一些配置文件,系统属性,或者一些方法调用的结果,对于前两种使用方式可以使用spring的PropertyPlaceholderConfigurer类来注入,这些内容已经在前面的文章中说过,这里就不在重复了。这里就针对第三种情况做一些说明,其实在spring中是提供了对这种需求的解决方案的,那就是使用org.springframework.beans.factory.config.MethodInvokingFactoryBean类来生成需要注入的bean的属性,下面是一个例子

 

MyBean.java一个普通的POJO类

  1. import org.apache.commons.lang.builder.ToStringBuilder;  
  2. public class MyBean {  
  3.     private String name;  
  4.     private String javaVersion;  
  5.       
  6.     public String getName() {  
  7.         return name;  
  8.     }  
  9.     public void setName(String name) {  
  10.         this.name = name;  
  11.     }  
  12.     public String getJavaVersion() {  
  13.         return javaVersion;  
  14.     }  
  15.     public void setJavaVersion(String javaVersion) {  
  16.         this.javaVersion = javaVersion;  
  17.     }  
  18.     @Override  
  19.     public String toString() {  
  20.         return ToStringBuilder.reflectionToString(this);  
  21.     }  
  22. }  
 

 

MyBeanNameProvider.java提供一个静态方法用来生成MyBean类的name属性,同理非静态方法也是可以的,参考本例子中javaVersion属性定义

  1. public class MyBeanNameProvider {  
  2.     public static String getName() {  
  3.         return "" + System.currentTimeMillis();  
  4.     }  
  5. }  
 

 

spring.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  6.                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"  
  7.        default-lazy-init="true">  
  8.     <bean id="myBean" class="MyBean">  
  9.         <property name="name"><ref local="myBeanName"/></property>  
  10.         <property name="javaVersion"><ref local="javaVersion"/></property>  
  11.     </bean>  
  12.     <bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
  13.         <property name="targetClass">  
  14.             <value>java.lang.System</value>  
  15.         </property>  
  16.         <property name="targetMethod">  
  17.             <value>getProperties</value>  
  18.         </property>  
  19.     </bean>  
  20.     <bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
  21.         <property name="targetObject">  
  22.             <ref local="sysProps" />  
  23.         </property>  
  24.         <property name="targetMethod">  
  25.             <value>getProperty</value>  
  26.         </property>  
  27.         <property name="arguments">  
  28.             <list>  
  29.                 <value>java.version</value>  
  30.             </list>  
  31.         </property>  
  32.     </bean>  
  33.       
  34.      <bean id="myBeanName" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
  35.         <property name="staticMethod"><value>MyBeanNameProvider.getName</value></property>  
  36.     </bean>  
  37. </beans>  
 

 

Test.java一个测试类

  1. import org.springframework.context.ApplicationContext;  
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  3. public class Test {  
  4.     public static void main(String[] args) {  
  5.         ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");  
  6.         MyBean myBean = (MyBean) ctx.getBean("myBean");  
  7.         System.out.println(myBean);  
  8.     }  
  9. }  
 

 

运行Test类,就可以看到MyBean的两个属性都通过spring配置的方式注入到了类中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值