Spring为属性注入某个类的常量或方法的返回值

spring提供了filed的值注入和method的返回值注入。

1、Field值的注入

filed值注入需要使用org.springframework.beans.factory.config.FieldRetrievingFactoryBean来获取类的静态变量。

例如,我们通常在接口中定义常量:

[java]  view plain copy
  1. package com.baobaotao.fb;  
  2. public interface CarBrandType {  
  3.     String HONG_QI = "红旗";  
  4.     String JI_LI = "吉利";  
  5. }  

下面利用FieldRetrievingFactoryBean获取CarBandType接口中定义的常量,并注入到某个bean的属性中:

[xhtml]  view plain copy
  1. <bean id="car" class="com.baobaotao.fb.Car">  
  2.     <property name="brand" ref="hongQi"/>  
  3. </bean>  
  4. <bean id="hongQi" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"  
  5.     <property name="staticField" value="com.baobaotao.fb.CarBrandType.HONG_QI"/>  
  6. </bean>  

 

Spring还允许用户用常量的全限定名作为FieldRetrievingFactoryBean的id,其效果和通过配置staticField属性是一样的:

[xhtml]  view plain copy
  1. <bean id="car" class="com.baobaotao.fb.Car">  
  2.     <property name="brand" ref="com.baobaotao.fb.CarBrandType.HONG_QI"/>  
  3. </bean>  
  4. <bean id="com.baobaotao.fb.CarBrandType.HONG_QI" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />  

 

当然,也可以直接将FieldRetrievingFactoryBean以内置bean的方式对brand属性进行赋值,这样在配置上更紧凑一些:

[xhtml]  view plain copy
  1. <bean id="car" class="com.baobaotao.fb.Car">  
  2.     <property name="brand">  
  3.         <bean id="com.baobaotao.fb.CarBrandType.HONG_QI" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">  
  4.         </bean>  
  5.     </property>  
  6. </bean>  

 //-------------------------------------------------------------

public interface Person {
 public static final int BOSS_LEVEL = 5;
 public static final int MANAGE_LEVEL = 2;
 public static final int STAFF_LEVEL = 1;
//以下省略
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
          <!-- 将person接口中的常量 ,赋值给相应的bean,通过引用可以获取程序中的常量 -->
          < bean  id = "BOSS_LEVEL_BEAN"  class = "org.springframework.beans.factory.config.FieldRetrievingFactoryBean" >
            < property  name = "staticField"  value = "person.member.Person.BOSS_LEVEL" />
          </ bean >
          < bean  id = "MANAGE_LEVEL_BEAN"  class = "org.springframework.beans.factory.config.FieldRetrievingFactoryBean" >
            < property  name = "staticField"  value = "person.member.Person.MANAGE_LEVEL" />
          </ bean >
          < bean  id = "STAFF_LEVEL_BEAN"  class = "org.springframework.beans.factory.config.FieldRetrievingFactoryBean" >
            < property  name = "staticField"  value = "person.member.Person.STAFF_LEVEL" />
          </ bean >
          <!-- 第二种方式也可以,感觉速度有点慢  也是通过引用-->
          <!-- 
          <bean id="person.member.Person.MANAGE_LEVEL" 
              class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
          <bean id="person.member.Person.BOSS_LEVEL"
                class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
          <bean id="person.member.Person.STAFF_LEVEL"
                class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
            --> 
         < bean  id = "bossMessage"  class = "person.attribute.BaseMessage" >
             < property  name = "userName"  value = "老板" />
             < property  name = "age"    value = "30"  />
         </ bean >
         < bean  id = "bossJob"  class = "person.attribute.Job" >
             < property  name = "jobName"  value = "takeMoney" />
             < property  name = "salary"  value = "20000"  />
             < property  name = "level"  ref = "BOSS_LEVEL_BEAN" ></ property >
         </ bean >
         < bean  id = "manageMessage"  class = "person.attribute.BaseMessage"  scope = "prototype" >
             < property  name = "userName"  value = "管理者" />
             < property  name = "age"    value = "25"  />
         </ bean >
         < bean  id = "manageJob"  class = "person.attribute.Job"  scope = "prototype" >
             < property  name = "jobName"  value = "management" />
             < property  name = "salary"  value = "10000"  />
             < property  name = "level"  ref = "MANAGE_LEVEL_BEAN" ></ property >
         </ bean >
         < bean  id = "staffMessage"  class = "person.attribute.BaseMessage" >
             < property  name = "userName"  value = "职员" />
             < property  name = "age"    value = "20"  />
         </ bean >
         < bean  id = "staffJob"  class = "person.attribute.Job" >
             < property  name = "jobName"  value = "work" />
             < property  name = "salary"  value = "5000"  />
             < property  name = "level"  ref = "STAFF_LEVEL_BEAN" ></ property >
         </ bean >


2、Method返回值的注入
Method返回值的注入需要使用MethodInvokingFactoryBean来完成。

在xml配置中,需要设定targetObject和targetMethod来指定目标bean和方法。如果使用静态方法,则需要指定targetClass和targetMethod

配置文件如下:

[xhtml]  view plain copy
  1. <bean id="bdog" class="com.baobaotao.fb.Dog">  
  2.   <property name="age">  
  3.     <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
  4.       <property name="targetObject">  
  5.         <ref local="randomAge"/>  
  6.       </property>  
  7.       <property name="targetMethod">  
  8.         <value>getAge</value>  
  9.       </property>  
  10.     </bean>  
  11.   </property>  
  12. </bean>  
  13. <bean id="randomAge" class="com.baobaotao.fd.RandomAge" />  
 
 

对于静态方法:

[xhtml]  view plain copy
  1. <bean id="bdog" class="com.baobaotao.fb.Dog">  
  2.   <property name="age">  
  3.     <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
  4.       <property name="targetClass">  
  5.         <value>com.baobaotao.fb.RandomAge</value>  
  6.       </property>  
  7.       <property name="targetMethod">  
  8.         <!-- getAge必须是静态方法 -->  
  9.         <value>getAges</value>  
  10.       </property>  
  11.     </bean>  
  12.   </property>  
  13. </bean>  


使用静态方法的返回值注入的另一种方式:

[html]  view plain copy
  1. <bean id="config"    
  2.         class="Configuration" factory-method="getInstance">    
  3.         <!-- 配置文件路径 -->    
  4.         <constructor-arg type="java.lang.String" value="config.properties"/>    
  5. </bean>   

运用 factory-method创建单例 ,getInstance指定的是静态方法,而不是使用构造函数来创建类的实例

[java]  view plain copy
  1. private static Configuration _instance = null;    
  2.     
  3. private Configuration(String configPath){    
  4.                 //执行你配置文件的加载    
  5.         System.out.println("configPath");    
  6.     }    
  7.         
  8.     public static Configuration getInstance(String configPath){    
  9.         logger.info("init...");    
  10.         if(_instance == null){    
  11.             _instance = new Configuration(configPath);    
  12.         }    
  13.         return _instance;    
  14. }    

你可能感兴趣的:(Spring为属性注入某个类的常量或方法的返回值)