【问题标题】:Spring Expression Language - get system property in Annotation [duplicate]Spring Expression Language - 在注释中获取系统属性[重复]
【发布时间】:2016-04-13 17:57:21
【问题描述】:

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

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

这是在抛出org.springframework.expression.spel.SpelParseException。 我试过了

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

但结果相同exception

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

【问题讨论】:

  • 您不应该使用#{systemProperties 而不是${systemProperties 吗?
  • 试过了,但得到了同样的异常
  • @Suganthan 尝试给出的示例并让我知道。在本地它有效,否则如果不正确,我将删除答案。

标签: java spring spring-mvc


【解决方案1】:

尝试以下方法:

@Component
public class WhateverBean {

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

    public String getUserName() {
        return userName;
    }

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

【讨论】:

    猜你喜欢
    • 2016-02-29
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多