当想把以下注解配置的代码转换成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>
还需按自己需要配置日志文件