spring提供了filed的值注入和method的返回值注入。
1、Field值的注入
filed值注入需要使用org.springframework.beans.factory.config.FieldRetrievingFactoryBean来获取类的静态变量。
例如,我们通常在接口中定义常量:
[java] view plain copy print ?
- package com.baobaotao.fb;
- public interface CarBrandType {
- String HONG_QI = "红旗";
- String JI_LI = "吉利";
- }
package com.baobaotao.fb; public interface CarBrandType { String HONG_QI = "红旗"; String JI_LI = "吉利"; }
下面利用FieldRetrievingFactoryBean获取CarBandType接口中定义的常量,并注入到某个bean的属性中:
[xhtml] view plain copy print ?
- <bean id="car" class="com.baobaotao.fb.Car">
- <property name="brand" ref="hongQi"/>
- </bean>
- <bean id="hongQi" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"
- <property name="staticField" value="com.baobaotao.fb.CarBrandType.HONG_QI"/>
- </bean>
<bean id="car" class="com.baobaotao.fb.Car"> <property name="brand" ref="hongQi"/> </bean> <bean id="hongQi" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" <property name="staticField" value="com.baobaotao.fb.CarBrandType.HONG_QI"/> </bean>
Spring还允许用户用常量的全限定名作为FieldRetrievingFactoryBean的id,其效果和通过配置staticField属性是一样的:
[xhtml] view plain copy print ?
- <bean id="car" class="com.baobaotao.fb.Car">
- <property name="brand" ref="com.baobaotao.fb.CarBrandType.HONG_QI"/>
- </bean>
- <bean id="com.baobaotao.fb.CarBrandType.HONG_QI" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
<bean id="car" class="com.baobaotao.fb.Car"> <property name="brand" ref="com.baobaotao.fb.CarBrandType.HONG_QI"/> </bean> <bean id="com.baobaotao.fb.CarBrandType.HONG_QI" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
当然,也可以直接将FieldRetrievingFactoryBean以内置bean的方式对brand属性进行赋值,这样在配置上更紧凑一些:
[xhtml] view plain copy print ?
- <bean id="car" class="com.baobaotao.fb.Car">
- <property name="brand">
- <bean id="com.baobaotao.fb.CarBrandType.HONG_QI" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
- </bean>
- </property>
- </bean>
<bean id="car" class="com.baobaotao.fb.Car"> <property name="brand"> <bean id="com.baobaotao.fb.CarBrandType.HONG_QI" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"> </bean> </property> </bean>
2、Method返回值的注入
Method返回值的注入需要使用MethodInvokingFactoryBean来完成。
在xml配置中,需要设定targetObject和targetMethod来指定目标bean和方法。如果使用静态方法,则需要指定targetClass和targetMethod
配置文件如下:
[xhtml] view plain copy print ?
- <bean id="bdog" class="com.baobaotao.fb.Dog">
- <property name="age">
- <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
- <property name="targetObject">
- <ref local="randomAge"/>
- </property>
- <property name="targetMethod">
- <value>getAge</value>
- </property>
- </bean>
- </property>
- </bean>
- <bean id="randomAge" class="com.baobaotao.fd.RandomAge" />
<bean id="bdog" class="com.baobaotao.fb.Dog"> <property name="age"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"> <ref local="randomAge"/> </property> <property name="targetMethod"> <value>getAge</value> </property> </bean> </property> </bean> <bean id="randomAge" class="com.baobaotao.fd.RandomAge" />
对于静态方法:
[xhtml] view plain copy print ?
- <bean id="bdog" class="com.baobaotao.fb.Dog">
- <property name="age">
- <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
- <property name="targetClass">
- <value>com.baobaotao.fb.RandomAge</value>
- </property>
- <property name="targetMethod">
-
- <value>getAges</value>
- </property>
- </bean>
- </property>
- </bean>
<bean id="bdog" class="com.baobaotao.fb.Dog"> <property name="age"> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass"> <value>com.baobaotao.fb.RandomAge</value> </property> <property name="targetMethod"> <!-- getAge必须是静态方法 --> <value>getAges</value> </property> </bean> </property> </bean>
使用静态方法的返回值注入的另一种方式:
[html] view plain copy print ?
- <bean id="config"
- class="Configuration" factory-method="getInstance">
-
- <constructor-arg type="java.lang.String" value="config.properties"/>
- </bean>
<bean id="config" class="Configuration" factory-method="getInstance"> <!-- 配置文件路径 --> <constructor-arg type="java.lang.String" value="config.properties"/> </bean>
运用 factory-method创建单例 ,getInstance指定的是静态方法,而不是使用构造函数来创建类的实例
[java] view plain copy print ?
- private static Configuration _instance = null;
-
- private Configuration(String configPath){
-
- System.out.println("configPath");
- }
-
- public static Configuration getInstance(String configPath){
- logger.info("init...");
- if(_instance == null){
- _instance = new Configuration(configPath);
- }
- return _instance;
- }