項目結構:
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配置文件來調用指定的方法,並且獲取方法飛返回值。