1. 程式人生 > >Spring XML獲取靜態方法返回值

Spring XML獲取靜態方法返回值

當想把以下註解配置的程式碼轉換成XML時發現自己忘了XML檔案怎麼獲取靜態方法返回值,主要是初學時不太瞭解原始碼,現在是要填坑了。

WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(webApplicationContext.getServletContext());
下面是幾個常用的xml配置FactoryBean(所屬包org.springframework.beans.factory.config,FactoryBean的實際生成bean為FactoryBean中getObject方法的返回值):

PropertyPathFactoryBean:獲取例項bean屬性值

FieldRetrievingFactoryBean:獲取欄位值

MethodInvokingFactoryBean:獲取方法返回值

PropertiesFactoryBean:獲取資原始檔Bean例項

該文章主要談論獲取方法返回值,所以對應的是MethodInvokingFactoryBean,進入該類後發現並沒有什麼屬性,找到其祖先類MethodInvoker,主要的配置屬性都在裡面,至於配置的詳細流程可以到MethodInvokingFactoryBean的getObject方法中追根溯源瞭解

MethodInvoker.class


可以根據自己所需的返回值配置屬性,如要呼叫例項方法則配置targetObject與targetMethod,呼叫靜態方法則配置staticMethod即可(配置了targetClass也無關係不會報錯),獲取靜態方法返回值的xml配置示例如下紅色方框,由於已配置了所屬類所以無需配置targetClass,若配置了targetClass省略方法的所屬包名依舊會報錯(該錯誤來自文章最後的例子)java.lang.IllegalStateException: Failed to load ApplicationContext
...............................
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'num' defined in class path resource [spring-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: staticMethod must be a fully qualified class plus method name: e.g. 'example.MyExampleClass.myExampleMethod'

... 25 more
Caused by: java.lang.IllegalArgumentException: staticMethod must be a fully qualified class plus method name: e.g. 'example.MyExampleClass.myExampleMethod'
........................................
... 42 more



StaticMethodObject.java:

package common;


public class StaticMethodObject {
	private int number;


	public int getNumber() {
		return number;
	}


	static public int getNum() {
		return 1;
	}


	public void setNumber(int num) {
		System.out.println(num);
		this.number = num;
	}
}

InvokeStaticMethodTest.java:

package common;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/spring-test.xml")
public class InvokeStaticMethodTest {
	@Autowired
	StaticMethodObject testObject;
	Logger log = Logger.getLogger(InvokeStaticMethodTest.class);


	@Test
	public void test() {
		log.debug("除錯輸出:" + testObject.getNumber());
	}
}
spring-test.xml

<?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:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="invokeTarget" class="common.StaticMethodObject" p:number-ref="num" depends-on="num"></bean>
		
	<bean id="num" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
		p:staticMethod="common.StaticMethodObject.getNum">
		<!-- p:staticMethod="common.StaticMethodObject.getNum" p:targetClass="common.StaticMethodObject"不報錯
			p:staticMethod="getNum" p:targetClass="common.StaticMethodObject" 報錯-->
	</bean>
</beans>


還需按自己需要配置日誌檔案