首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpEL getMethod不工作

SpEL getMethod不工作
EN

Stack Overflow用户
提问于 2014-09-25 02:55:49
回答 1查看 1.2K关注 0票数 0

假设我有一个类foo.PoJo,它有一个静态的公共方法字符串,根据官方参考,这段代码应该返回一个方法实例。

代码语言:javascript
复制
SpelExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("T(foo.PoJo).getMethod('print')");
Method m = (Method) exp.getValue();

但是,此代码将引发一个例外:

代码语言:javascript
复制
  Exception in thread "main" org.springframework.expression.spel.SpelEvaluationException: EL1004E:(pos 49): Method call: Method getMethod(java.lang.String) cannot be found on foo
.PoJo type
        at org.springframework.expression.spel.ast.MethodReference.findAccessorForMethod(MethodReference.java:185)
        at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:107)
        at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:57)
        at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93)
        at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:66)
        at com.duowan.realtime.scheduled.batch.temp.PoJo.main(PoJo.java:57)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我的猜测是T()操作将返回一个Class对象,因此您可以访问这个对象的静态方法和字段,但是方法getMethod(String,Class.parameterTypes)函数只是一个公共方法,而不是静态的,所以会引发异常。如果我是对的,这个推荐信可能需要更新。有人知道吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-25 05:57:14

T()实际上返回类实例,您可以访问在Foo上声明的静态方法。在Java中,您必须使用Foo.class.getMethod()

看起来Spring在3.1中增加了对访问类方法的支持;我只有在使用3.0.x时才会看到您的失败。

当前的版本是4.1.0。

编辑:

确认-固定在3.1.3

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26029760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档