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

项目结构:

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

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

 1 package com.b510.app.test;
2
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6 import com.b510.service.AnimalService;
7
8 public class SpringTest {
9 public static void main(String[] args) {
10 ApplicationContext act=new ClassPathXmlApplicationContext("beans.xml");
11 //dogServiceBean是通过普通方法,获取到值
12 AnimalService dogServiceBean=(AnimalService) act.getBean("dog1");
13 System.out.println("获取dog1的年龄为:"+dogServiceBean.getAge());
14 //dogServiceBean2是通过静态方法,获取到值
15 AnimalService dogServiceBean2=(AnimalService) act.getBean("dog2");
16 System.out.println("获取dog2的年龄为:"+dogServiceBean2.getAge());
17 //javaVersion=sysProps.getProperty("java.version");
18 String info=(String) act.getBean("javaVersion");
19 System.out.println("系统的java版本是:"+info);
20 }
21 }

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

 1 package com.b510.app.util;
2
3 /**
4 * 值的生产者
5 *
6 * @author Hongten
7 *
8 */
9 public class ValueGenerator {
10
11 /**
12 * 定义一个普通的方法,获取年龄
13 *
14 * @return 返回一个int类型的值
15 */
16 public int getAgeValue() {
17 return 23;
18 }
19
20 /**
21 * 定义一个静态方法,获取年龄
22 *
23 * @return 返回一个int类型的值
24 */
25 public static int getAgeStaticValue() {
26 return 20;
27 }
28 }

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

 1 package com.b510.service;
2
3 public interface AnimalService {
4
5 /**
6 * 定义一个抽象方法setAge
7 *
8 * @param age
9 * 年龄
10 */
11 public abstract void setAge(int age);
12
13 /**
14 * 定义一个抽象方法getAge
15 *
16 * @return 一个int类型的值
17 */
18 public abstract int getAge();
19
20 }

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

 1 package com.b510.service.impl;
2
3 import com.b510.service.AnimalService;
4
5 /**
6 * 定义DogServiceBean类
7 *
8 * @author Hongten
9 *
10 */
11 public class DogServiceBean implements AnimalService {
12 /**
13 * 年龄
14 */
15 private int age;
16
17 public int getAge() {
18 return age;
19 }
20
21 public void setAge(int age) {
22 this.age = age;
23 }
24
25 }

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

也可以是实例方法。

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

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

 1 <?xml version="1.0" encoding="GBK"?>
2 <!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
3 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns="http://www.springframework.org/schema/beans"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
7 <!-- 定义目标Bean,后面将会获取该Bean的方法返回值 -->
8 <bean id="valueGenerator" class="com.b510.app.util.ValueGenerator"></bean>
9 <!-- 定义dog1的bean -->
10 <bean id="dog1" class="com.b510.service.impl.DogServiceBean">
11 <property name="age">
12 <bean
13 class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
14 <!-- targetObject确定目标Bean,指定调用哪个Bean -->
15 <property name="targetObject" ref="valueGenerator" />
16 <!-- targetMethod确定目标方法,指定调用目标Bean的哪个方法 -->
17 <property name="targetMethod" value="getAgeValue" />
18 </bean>
19 </property>
20 </bean>
21 <!-- 定义名为dog2的Bean -->
22 <bean id="dog2" class="com.b510.service.impl.DogServiceBean">
23 <property name="age">
24 <bean
25 class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
26 <!-- targetClass确定目标类,指定调用哪个类 -->
27 <property name="targetClass" value="com.b510.app.util.ValueGenerator" />
28 <!-- targetMethod确定目标方法,指定调用目标class的哪个方法。
29 该方法必须是静态方法-->
30 <property name="targetMethod" value="getAgeStaticValue"></property>
31 </bean>
32 </property>
33 </bean>
34
35
36 <!-- 将静态方法返回值直接定义成Bean -->
37 <bean id="sysProps"
38 class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
39 <!-- targetClass确定目标类,确定调用哪个类 -->
40 <property name="targetClass" value="java.lang.System" />
41 <!-- targetMethod确定目标方法,确定调用目标class的哪个方法
42 该方法必须是静态方法-->
43 <property name="targetMethod" value="getProperties" />
44 </bean>
45 <!-- 将实例方法返回值直接定义成Bean -->
46 <bean id="javaVersion"
47 class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
48 <!-- targetObject确定目标Bean,确定调用哪个Bean -->
49 <property name="targetObject" ref="sysProps" />
50 <!-- targetMethod确定目标方法,确定调用目标Bean的哪个方法 -->
51 <property name="targetMethod" value="getProperty" />
52 <!-- 确定调用目标方法的参数 -->
53 <property name="arguments">
54 <!-- list元素列出调用方法多个参数值 -->
55 <list>
56 <value>java.version</value>
57 </list>
58 </property>
59 </bean>
60 </beans>

运行结果:

 1 2012-3-12 12:12:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh
2 信息: 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
3 2012-3-12 12:12:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
4 信息: Loading XML bean definitions from class path resource [beans.xml]
5 2012-3-12 12:12:54 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
6 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1a05308]: org.springframework.beans.factory.support.DefaultListableBeanFactory@bfc8e0
7 2012-3-12 12:12:54 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
8 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@bfc8e0: defining beans [valueGenerator,dog1,dog2,sysProps,javaVersion]; root of factory hierarchy
9 获取dog1的年龄为:23
10 获取dog2的年龄为:20
11 系统的java版本是:1.6.0_22

Spring提供的MethodInvokingFactoryBean功能很强大,通过这个工厂Bean,我们可以通过Spring配置文件来调用指定的方法,并且获取方法飞返回值。

原文地址:https://www.cnblogs.com/hongten/p/java_spring_MethodInvokingFactoryBean.html