Spring表达式语言 - 在注释

时间:2016-04-13 09:39:21

标签: java spring spring-mvc

在我的控制器中,我试图在Spring注释中获得system properties。这是代码

@PreAuthorize("hasPermission('${systemProperties['user.name']}', '')")

投掷org.springframework.expression.spel.SpelParseException。 我试过了

@PreAuthorize("hasPermission('#{systemProperties['user.name']}', '')")

但结果相同exception

在Spring注释中获取系统属性的语法是什么?

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

@Component
public class WhateverBean {

    @Value("#{systemProperties['user.name']}")
    private String userName;

    public String getUserName() {
        return userName;
    }

    @PreAuthorize("hasPermission('@whateverBean.getUserName()', '')")
    public void xxx() {
        // ...
    }
}