您的位置:首页 > 编程语言 > Java开发

spring开发_注入其他Bean的方法返回值_MethodInvokingFactoryBean

2012-03-12 12:29 731 查看
项目结构:

http://www.cnblogs.com/hongten/gallery/image/112562.html

/spring_1300_注入其他Bean的方法返回值/src/com/b510/app/test/SpringTest.java

package com.b510.app.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.b510.service.AnimalService;

public class SpringTest {
public static void main(String[] args) {
ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml");
//dogServiceBean是通过普通方法,获取到值
AnimalService dogServiceBean=(AnimalService) act.getBean("dog1");
System.out.println("获取dog1的年龄为:"+dogServiceBean.getAge());
//dogServiceBean2是通过静态方法,获取到值
AnimalService dogServiceBean2=(AnimalService) act.getBean("dog2");
System.out.println("获取dog2的年龄为:"+dogServiceBean2.getAge());
//javaVersion=sysProps.getProperty("java.version");
String info=(String) act.getBean("javaVersion");
System.out.println("系统的java版本是:"+info);
}
}


/spring_1300_注入其他Bean的方法返回值/src/com/b510/app/util/ValueGenerator.java

package com.b510.app.util;

/**
* 值的生产者
*
* @author Hongten
*
*/
public class ValueGenerator {

/**
* 定义一个普通的方法,获取年龄
*
* @return 返回一个int类型的值
*/
public int getAgeValue() {
return 23;
}

/**
* 定义一个静态方法,获取年龄
*
* @return 返回一个int类型的值
*/
public static int getAgeStaticValue() {
return 20;
}
}


/spring_1300_注入其他Bean的方法返回值/src/com/b510/service/AnimalService.java

package com.b510.service;

public interface AnimalService {

/**
* 定义一个抽象方法setAge
*
* @param age
*            年龄
*/
public abstract void setAge(int age);

/**
* 定义一个抽象方法getAge
*
* @return 一个int类型的值
*/
public abstract int getAge();

}


/spring_1300_注入其他Bean的方法返回值/src/com/b510/service/impl/DogServiceBean.java

package com.b510.service.impl;

import com.b510.service.AnimalService;

/**
* 定义DogServiceBean类
*
* @author Hongten
*
*/
public class DogServiceBean implements AnimalService {
/**
* 年龄
*/
private int age;

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}


通过MethodInvokingFactoryBean工厂Bean,可以将指定方法返回值注入成为目标Bean的属性值,MethodInvokingFactoryBean用来获得指定方法的返回值,该方法可以是静态方法

也可以是实例方法。

获得的方法返回值既可以被注入到指定Bean实例的指定属性,也可以直接定义成Bean实例。

/spring_1300_注入其他Bean的方法返回值/src/beans.xml

<?xml version="1.0" encoding="GBK"?>
<!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 定义目标Bean,后面将会获取该Bean的方法返回值 -->
<bean id="valueGenerator" class="com.b510.app.util.ValueGenerator"></bean>
<!-- 定义dog1的bean -->
<bean id="dog1" class="com.b510.service.impl.DogServiceBean">
<property name="age">
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<!-- targetObject确定目标Bean,指定调用哪个Bean -->
<property name="targetObject" ref="valueGenerator" />
<!-- targetMethod确定目标方法,指定调用目标Bean的哪个方法 -->
<property name="targetMethod" value="getAgeValue" />
</bean>
</property>
</bean>
<!-- 定义名为dog2的Bean -->
<bean id="dog2" class="com.b510.service.impl.DogServiceBean">
<property name="age">
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<!-- targetClass确定目标类,指定调用哪个类 -->
<property name="targetClass" value="com.b510.app.util.ValueGenerator" />
<!-- targetMethod确定目标方法,指定调用目标class的哪个方法。
该方法必须是静态方法-->
<property name="targetMethod" value="getAgeStaticValue"></property>
</bean>
</property>
</bean>

<!--  将静态方法返回值直接定义成Bean -->
<bean id="sysProps"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<!-- targetClass确定目标类,确定调用哪个类 -->
<property name="targetClass" value="java.lang.System" />
<!-- targetMethod确定目标方法,确定调用目标class的哪个方法
该方法必须是静态方法-->
<property name="targetMethod" value="getProperties" />
</bean>
<!-- 将实例方法返回值直接定义成Bean -->
<bean id="javaVersion"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<!-- targetObject确定目标Bean,确定调用哪个Bean -->
<property name="targetObject" ref="sysProps" />
<!-- targetMethod确定目标方法,确定调用目标Bean的哪个方法 -->
<property name="targetMethod" value="getProperty" />
<!-- 确定调用目标方法的参数 -->
<property name="arguments">
<!-- list元素列出调用方法多个参数值 -->
<list>
<value>java.version</value>
</list>
</property>
</bean>
</beans>


运行结果:

2012-3-12 12:12:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1a05308: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1a05308]; startup date [Mon Mar 12 12:12:51 CST 2012]; root of context hierarchy
2012-3-12 12:12:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2012-3-12 12:12:54 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1a05308]: org.springframework.beans.factory.support.DefaultListableBeanFactory@bfc8e0
2012-3-12 12:12:54 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@bfc8e0: defining beans [valueGenerator,dog1,dog2,sysProps,javaVersion]; root of factory hierarchy
获取dog1的年龄为:23
获取dog2的年龄为:20
系统的java版本是:1.6.0_22


Spring提供的MethodInvokingFactoryBean功能很强大,通过这个工厂Bean,我们可以通过Spring配置文件来调用指定的方法,并且获取方法飞返回值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: