假设我有一个类foo.PoJo,它有一个静态的公共方法字符串,根据官方参考,这段代码应该返回一个方法实例。
SpelExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("T(foo.PoJo).getMethod('print')");
Method m = (Method) exp.getValue();但是,此代码将引发一个例外:
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)函数只是一个公共方法,而不是静态的,所以会引发异常。如果我是对的,这个推荐信可能需要更新。有人知道吗?
发布于 2014-09-25 05:57:14
T()实际上返回类实例,您可以访问在Foo上声明的静态方法。在Java中,您必须使用Foo.class.getMethod()。
看起来Spring在3.1中增加了对访问类方法的支持;我只有在使用3.0.x时才会看到您的失败。
当前的版本是4.1.0。
编辑:
https://stackoverflow.com/questions/26029760
复制相似问题