将varargs传递给Spring spEL会导致“在com.sun.proxy上找不到方法”。 [英] Passing varargs to Spring spEL causes "Method cannot be found on com.sun.proxy"

查看:156
本文介绍了将varargs传递给Spring spEL会导致“在com.sun.proxy上找不到方法”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写用于权限评估的自定义类,因此它可以与Spring Security @PreAuthorize 和Spring Expression Language一起使用,例如this( authority 只是一个带有一些角色名称的常规Spring组件):

I try to write custom class for permissions evaluation, so it can be used with Spring Security @PreAuthorize and Spring Expression Language like for example this (authority is just a regular Spring component with some role names):

@PreAuthorize("@permissionEvaluator.anyOfRoles(@authority.ADMIN)")

PermissionEvaluator#anyOfRoles 方法声明如下所示:

boolean anyOfRoles(String... roles)

如图所示,此方法采用String类型的变量。只传递一个参数(如上例所示),但是使用多个参数调用它时,它工作正常,例如。

As shown this method takes varargs of String type. It works fine when only one parameter is passed (as on the example above) but calling it with more than one argument, eg.

@PreAuthorize("@permissionEvaluator.anyOfRoles(@authority.ADMIN, @authority.USER)")

原因

org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method anyOfRoles(java.lang.String,java.lang.String) cannot be found on com.sun.proxy.$Proxy132 type

是抛出。它在使用字符串数组调用时仍然有效(基本上与varargs相同,这只是语法糖),如下所示:

to be thrown. It still works when called with an array of Strings (basically same as varargs, which is just syntactic sugar), like this:

@PreAuthorize("@permissionEvaluator.anyOfRoles(new String[] { @authority.ADMIN, @authority.USER })")

我试图查看一些关于使用spEL传递varargs的其他信息,但Spring文档只是神秘地提及

I tried to look some additional info about passing varargs with spEL but Spring documentation only enigmatically mentions in spEL documentation that


也支持Varargs。

Varargs are also supported.

这个异常的原因可能是什么,除了在spEL中传递数组还有其他解决方法吗? / p>

What might be the cause of this exception and is there other workaround than passing array in spEL?

推荐答案

你使用的是什么版本的Spring?我刚用4.3.12运行这个测试用例并且工作正常......

What version of Spring are you using? I just ran this test case with 4.3.12 and it worked fine...

@SpringBootApplication
public class So46953884Application {

    public static void main(String[] args) {
        SpringApplication.run(So46953884Application.class, args);
    }

    @Value("#{foo.foo('a', 'b')}")
    private String foo;

    @Bean
    public ApplicationRunner runner() {
        return args -> System.out.println(foo);
    }

    @Bean
    public Foo foo() {
        return new Foo();
    }

    public static class Foo {

        public String foo(String... strings) {
            return "filled: " + Arrays.toString(strings);
        }

    }

}

结果:

filled: [a, b]

这篇关于将varargs传递给Spring spEL会导致“在com.sun.proxy上找不到方法”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆