简体   繁体   English

注释属性RequestMapping.value的值必须是常量表达式

[英]The value for annotation attribute RequestMapping.value must be a constant expression

When using the following code snippet: 使用以下代码段时:

public class MyUrls {

    // properties get initialized using static{...}
    public final static String URL_HOMEPAGE = properties.getProperty("app.homepage");    

}

@Controller
public class HomepageController {

    @RequestMapping(MyUrls.URL_HOMEPAGE)
    public String homepage() {
        return "/homepage/index";
    }

}

I get the following error: 我收到以下错误:

The value for annotation attribute RequestMapping.value must be a constant expression

But in fact, URL_HOMEPAGE does be a constant, since it is declared as public final static . 但事实上, URL_HOMEPAGE确实是一个常量,因为它被声明为public final static Am I wrong? 我错了吗? How to solve this issue? 如何解决这个问题?

Whilst URL_HOMEPAGE is a constant it's value may not be, it can only be determined at runtime. 虽然URL_HOMEPAGE是常量,但它的值可能不是,它只能在运行时确定。 I believe that values used in annotations must be resolvable at compile-time. 我相信注释中使用的值必须在编译时可解析。

It is a constant, but it is initialized after the request mapping is initialized. 它是一个常量,但在初始化请求映射后初始化。 You are calling properties.getProperty("app.homepage"); 您正在调用properties.getProperty("app.homepage"); When the classloader loads you class, the URL_HOMEPAGE is not initialized yet, hence the error. 当类加载器加载您的类时,URL_HOMEPAGE尚未初始化,因此错误。
You need to give as a parameter an initialized string, such as "/path/subpath" 您需要将初始化字符串作为参数提供,例如“/ path / subpath”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CsvBindByPosition:注释属性的值必须是常量表达式 - CsvBindByPosition : value for annotation attribute must be a constant expression annotation属性的值必须是常量表达式 - value for the annotation attribute must be constant expression 注释属性Query.value的值必须是一个常量表达式 - The value for annotation attribute Query.value must be a constant expression 注释属性Min.value的值必须是常量表达式 - The value for annotation attribute Min.value must be a constant expression 注释属性GetMapping.produces的值必须是一个常量表达式 - The value for annotation attribute GetMapping.produces must be a constant expression 为什么注释属性Rest.rootUrl的值必须是常量表达式? - Why The value for annotation attribute Rest.rootUrl must be a constant expression? 注释属性Test.enabled的值必须是一个常量表达式 - The value for annotation attribute Test.enabled must be a constant expression Java注解,属性值必须是常量 - Java annotation, Attribute value must be constant 为什么注释属性的值应该是常量表达式? - Why should the value for an annotation attribute be a constant expression? 通过函数构建端点值-注释属性X的值必须为常量表达式 - Building endpoint values from a function - The value for annotation attribute X must be a constant expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM