在Spring配置文件中使用XML文件進行配置,實際上是讓Spring執行了相應的代碼,例如:
使用<bean>元素,實際上是讓Spring執行無參或有參構造器
使用<property>元素,實際上是讓Spring執行一次setter方法
但Java程序還可能有其他類型的語句:調用getter方法、調用普通方法、訪問類或對象的Field等,而Spring也為這種語句提供了對應的配置語法:
調用getter方法:使用PropertyPathFactoryBean
調用類或對象的Filed值:使用FiledRetrievingFactoryBean
調用普通方法:使用MethodInvokingFactoryBean
注入其他Bean的屬性值
PropertyPathFactoryBean用來獲得目標Bean的屬性值(實際上就是調用getter方法返回的值),獲得的值可以注入給其他的Bean,也可以直接定義新的Bean。看如下的配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<bean id= "person" class = "com.abc.Person" > <property name= "age" value= "30" /> <property name= "son" > <!-- 使用嵌套Bean定義屬性值 --> <bean class = "com.abc.service.Son" > <property name= "age" value= "11" /> </bean> </property> </bean> <bean id= "son2" class = "com.abc.service.Son" > <!-- age屬性不是直接注入,而是將person中的son的age屬性賦值給son2的age屬性 --> <property name= "age" > <!-- 注意這里使用的是PropertyPathFactoryBean --> <bean id= "person.son.age" class = "org.springframework.beans.factory.config.PropertyPathFactoryBean" /> </property> </bean> |
其中Person類和Son類的屬性可以從配置文件中看出,這不再給出。主程序如下:
1
2
3
4
5
6
7
|
public class Test { public static void main(String args[]) { ApplicationContext ac = new ClassPathXmlApplicationContext( "applicationContext.xml" ); System.out.println( "age=" + ac.getBean( "son2" , Son. class ).getAge()); } } |
輸出結果:
age=11
Bean實例的屬性值,不僅可以注入另一個Bean,還可將Bean實例的屬性值直接定義成Bean實例,這也是通過PropertyPathFactoryBean完成的。對上面的配置文件增加這樣一段:
1
2
3
4
5
6
7
|
<bean id= "son1" class = "org.springframework.beans.factory.config.PropertyPathFactoryBean" > <!-- 確定目標Bean,表明son1來自哪個Bean的組件 --> <property name= "targetBeanName" value= "person" /> <!-- 確定屬性,表明son1來自目標Bean的哪個屬性 --> <property name= "propertyPath" value= "son" /> </bean> |
執行上面的Test類,把son2換成son1,結果一樣。
注入其他Bean的Field值
通過FieldRetrievingFactoryBean類,可以將其他Bean的Field值注入給其他Bean,或者直接定義新的Bean。下面是配置片段:
1
2
3
4
5
6
|
<bean id= "son" class = "com.abc.service.Son" > <property name= "age" > <bean id= "java.sql.connection.TRANSACTION_SERIALIZABLE" class = "org.springframework.beans.factory.config.FieldRetrievingFactoryBean" /> </property> </bean> |
測試主程序與上文定義的類似,這里不再提供,執行結果如下:
age=8
在這個配置中,son對象的age的值,等于java.sql.Connection.TRANSACTION_SERIALIZABLE的 值。在上面的定義中,定義FieldRetrievingFactoryBean工廠Bean時,指定的id并不是該Bean實例的唯一標識,而是指定 Field的表達式(即將要被取出來的值)。
注意:Field既可以是靜態的,也可以是非靜態的。上面的配置片段指定的Field表達式是靜態Field值,因此可以通過類名直接訪問。如 果Field值是非靜態的,則應該通過容器中已經存在的Bean來訪問——即Field表達式的第一個短語應該是容器中已經存在的Bean。
Field值也可以定義成Bean實例,例如,在配置文件中增加下面一段:
1
2
3
4
5
6
7
|
<bean id= "age" class = "org.springframework.beans.factory.config.FieldRetrievingFactoryBean" > <!-- targetClass指定Field所在的目標類 --> <property name= "targetClass" value= "java.sql.Connection" /> <!-- targetField指定Field名 --> <property name= "targetField" value= "TRANSACTION_SERIALIZABLE" /> </bean> |
在主程序中增加如下輸出:
System.out.println("age=" + ac.getBean("age"));
執行結果和上文一樣。
使用FieldRetrievingFactoryBean獲取Field值時,必須指定如下兩個屬性:
targetClass或targetObject:分別用于指定Field值所在的目標累或目標對象。如果需要獲得的Field是靜態的,則使用targetClass指定目標累;如果Field是非靜態的,則使用targetObject指定目標對象
targetField:指定目標類或目標對象的Field名
如果Field是個靜態Field,則有一種更加簡潔的寫法:
1
2
3
4
5
|
<bean id= "age" class = "org.springframework.beans.factory.config.FieldRetrievingFactoryBean" > <!-- value指定哪個類的哪個靜態域值 --> <property name= "staticField" value= "java.sql.Connection.TRANSACTION_SERIALIZABLE" /> </bean> |
注入其他Bean的方法返回值
通過MethodInvokingFactoryBean工廠Bean,可將目標方法的返回值注入為Bean的屬性值。這個工廠Bean用來獲 取指定方法的返回值,該方法既可以是靜態方法,也可以是實例方法;這個值既可以被注入到指定Bean實例的指定屬性,也可以直接定義成Bean實例。看例 子:
1
2
3
4
5
6
7
8
9
10
11
|
<bean id= "valueGenerator" class = "com.abc.util.ValueGenerator" /> <bean id= "son1" class = "com.abc.service.Son" > <property name= "age" > <!-- 獲取方法返回值:調用valueGenerator的getValue方法 --> <bean class = "org.springframework.beans.factory.config.MethodInvokingFactoryBean" > <property name= "targetObject" ref= "valueGenerator" /> <property name= "targetMethod" value= "getValue" /> </bean> </property> </bean> |
下面是ValueGenerator:
1
2
3
4
|
public class ValueGenerator { public int getValue() { return 2 ; } public static int getStaticValue () { return 3 ;} } |
測試程序依舊打印son1中age的值,代碼略,結果如下:
age=2
如果要調用靜態方法,則把配置修改為:
1
2
3
4
5
6
7
8
9
10
|
<bean id= "son1" class = "com.abc.service.Son" > <property name= "age" > <!-- 獲取方法返回值:調用valueGenerator的getStaticValue方法 --> <bean class = "org.springframework.beans.factory.config.MethodInvokingFactoryBean" > <property name= "targetClass" value= "com.abc.util.ValueGenerator" /> <property name= "targetMethod" value= "getStaticValue" /> </bean> </property> </bean> |
測試結果為:
age=3
由于Java是支持重載的,只給定方法名,還不足以能夠確定調用哪個方法,通過上面的配置能調用成功是因為ValueGenerator中的兩個方法都沒有參數。如果方法中有參數,該如何配置呢?在配置文件中加入以下內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<bean id= "sysProps" class = "org.springframework.beans.factory.config.MethodInvokingFactoryBean" > <property name= "targetClass" value= "java.lang.System" /> <property name= "targetMethod" value= "getProperties" /> <bean> <bean id= "javaVersion" class = "org.springframework.beans.factory.config.MethodInvokingFactoryBean" > <!-- 指向上面的sysProps Bean --> <property name= "targetObject" value= "sysProps" /> <property name= "targetMethod" value= "getProperty" /> <!-- 這里配置參數 --> <property name= "arguments" > <!-- 使用list元素列出調用方法的多個參數 --> <list> <value>java.version</value> </list> </property> <bean> |
上例中相當于用”java.version”作為參數調用了java.lang.System的getProperty方法,返回值將創建一個名為javaVersion的Bean。即相當于:
javaVersion = java.lang.System.getProperty("java.version");
和前文中的Field一樣,如果要調用的方法為靜態方法,也有一種更加簡潔的方法:
1
2
3
4
5
|
<bean id= "myBean" class = "org.springframework.beans.factory.config.MethodInvokingFactoryBean" > <!-- 使用staticMethod屬性,直接指定目標類的目標方法 --> <property name= "staticMethod" value= "com.abc.util.ValueGenerator.getStaticValue" /> </bean> |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。