项目结构:
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 >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配置文件来调用指定的方法,并且获取方法飞返回值。