Spring表达式无法解析类型

时间:2014-09-10 15:49:59

标签: java spring spring-security spring-el

我有一个用@PostAuthorize

注释的服务方法
@PostAuthorize("hasPermission(returnObject, new ReadUserPermission())")
public Optional<User> find(String email) {
    // implementation
}

一旦调用该方法,spring就会抛出以下异常:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1003E:(pos 28): A problem occurred whilst attempting to construct an object of type 'ReadUserPermission' using arguments '()'
    at org.springframework.expression.spel.ast.ConstructorReference.findExecutorForConstructor(ConstructorReference.java:190) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.ConstructorReference.createNewInstance(ConstructorReference.java:151) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.ConstructorReference.getValueInternal(ConstructorReference.java:94) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getArguments(MethodReference.java:147) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:79) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:113) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:105) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.security.access.expression.ExpressionUtils.evaluateAsBoolean(ExpressionUtils.java:11) ~[spring-security-core-3.1.2.RELEASE.jar:3.1.2.RELEASE]
    ... 86 common frames omitted
Caused by: org.springframework.expression.AccessException: Failed to resolve constructor
    at org.springframework.expression.spel.support.ReflectiveConstructorResolver.resolve(ReflectiveConstructorResolver.java:121) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.ast.ConstructorReference.findExecutorForConstructor(ConstructorReference.java:181) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    ... 93 common frames omitted
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1005E:(pos 0): Type cannot be found 'ReadUserPermission'
    at org.springframework.expression.spel.support.StandardTypeLocator.findType(StandardTypeLocator.java:81) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    at org.springframework.expression.spel.support.ReflectiveConstructorResolver.resolve(ReflectiveConstructorResolver.java:61) ~[spring-expression-4.0.0.RELEASE.jar:4.0.0.RELEASE]
    ... 94 common frames omitted

ReadUserPermission看起来像这样:

@Component
public class ReadUserPermission implements Permission<User> {

    @Override
    public boolean hasPermission(User user, User target) {
        // implementation
    }
}

首先,课程ReadUserPermission和界面Permission均未使用@Component进行注释。我想也许spring需要注释才能找到类型,但是如果我注释接口或类或两者都没关系。例外总是一样的。

1 个答案:

答案 0 :(得分:4)

我忽略了documentation的这一部分:

  

可以使用new运算符调用构造函数。 完全限定类名应该用于除基本类型和String之外的所有类型(可以使用int,float等)