项目中用到需要初始化一些数据,Spring提供了filed的值注入和method的返回值注入。
一、Field值的注入
filed值注入需要使用org.springframework.beans.factory.config.FieldRetrievingFactoryBean来获取类的静态变量。
例如,我们通常在接口中定义常量:
package com.wy.inject; /** * * @author wy * */ public interface Fruit { public String APPLE = "苹果"; public String ORANGE = "桔子"; }
下面利用FieldRetrievingFactoryBean获取CarBandType接口中定义的常量,并注入到某个bean的属性中:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <!-- 1、通过注入属性 <bean id="appleBean" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> <property name="staticField" value="com.wy.inject.Fruit.APPLE" /> </bean> <bean id="apple" class="com.wy.inject.Apple"> <property name="name" ref="appleBean" /> </bean>-->
Spring还允许用户用常量的全限定名作为FieldRetrievingFactoryBean的id,其效果和通过配置staticField属性是一样的:
<!-- 2、通过全限量名称 <bean id="com.wy.inject.Fruit.APPLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> </bean> <bean id="apple" class="com.wy.inject.Apple"> <property name="name" ref="com.wy.inject.Fruit.APPLE" /> </bean> -->
当然,也可以直接将FieldRetrievingFactoryBean以内置bean的方式对属性进行赋值,这样在配置上更紧凑一些:
<!-- 3、通过内置Bean的方式 <bean id="apple" class="com.wy.inject.Apple"> <property name="name"> <bean id="com.wy.inject.Fruit.APPLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/> </property> </bean>-->
二、Method返回值的注入
Method返回值的注入需要使用MethodInvokingFactoryBean来完成。
在xml配置中,需要设定targetObject和targetMethod来指定目标bean和方法。如果使用静态方法,则需要指定targetClass和targetMethod。
1、非静态方法配置文件如下:
<!-- 1、Method返回值的注入 <bean id="apple" class="com.wy.inject.Apple"> <property name="name"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <ref local="cons" /> </property> <property name="targetMethod"> <value>getAPPLE</value> </property> </bean> </property> </bean> <bean id="cons" class="com.wy.inject.Cons" /> -->
2、静态方法的配置
<!-- 2、静态方法返回值的注入 --> <bean id="banna" class="com.wy.inject.Banna"> <property name="name"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass"> <value>com.wy.inject.Cons</value> </property> <property name="targetMethod"> <!-- getBann必须是静态方法 --> <value>getBann</value> </property> </bean> </property> </bean>
涉及到的类:
Apple.java
package com.wy.inject; /** * * @author wy * */ public class Apple { String name = null; String price = null; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
Cons.java
package com.wy.inject; /** * * @author wy * */ public class Cons { public String APPLE = "苹果"; public String ORANGE = "桔子"; public static String Bann = "香蕉"; public String getAPPLE() { return APPLE; } public void setAPPLE(String aPPLE) { APPLE = aPPLE; } public String getORANGE() { return ORANGE; } public void setORANGE(String oRANGE) { ORANGE = oRANGE; } public static String getBann() { return Bann; } public static void setBann(String bann) { Bann = bann; } }
Banna.java
package com.wy.inject; /** * * @author wy * */ public class Banna { String name = null; String price = null; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
三、测试例子
package com.wy.inject; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class InjectTest { public Apple apple = null; public Banna banna = null; public InjectTest() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // apple = (Apple)context.getBean("apple"); banna = (Banna)context.getBean("banna"); } public void name(){ // System.out.println("Fruit name=" + apple.getName()); System.out.println("Fruit name=" + banna.getName()); } /** * @param args */ public static void main(String[] args) { new InjectTest().name(); } }