Java SpEL getMethod don';行不通
假设我有一个类foo.PoJo,它有一个静态公共方法字符串,根据,这段代码应该返回一个方法实例Java SpEL getMethod don';行不通,java,spring,spring-el,Java,Spring,Spring El,假设我有一个类foo.PoJo,它有一个静态公共方法字符串,根据,这段代码应该返回一个方法实例 SpelExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression("T(foo.PoJo).getMethod('print')"); Method m = (Method) exp.getValue(); 但是,此代码会引发异常: Exception in thre
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()操作将返回一个类对象,因此您可以访问该对象的静态方法和字段,但是方法getMethod(String name,Class…parameterTypes)函数只是一个公共方法,而不是静态的,因此引发了异常。
如果我是对的,参考资料可能需要更新。有人知道吗?T()
实际上返回类实例,您可以访问在Foo
本身上声明的静态方法。在Java中,必须使用Foo.class.getMethod()
看起来Spring在3.1中增加了对访问类方法的支持;我只知道你在使用3.0.x时失败了
目前的Spring框架版本是4.1.0
编辑:
.您发布的代码适用于我的Spring 4.x。