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


问题内容

使用以下代码段时:

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";
    }

}

我收到以下错误:

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

但实际上,URL_HOMEPAGE确实是一个常量,因为它被声明为public final static。我错了吗?如何解决这个问题?


问题答案:

尽管URL_HOMEPAGE它是一个常量,但它的值可能不是,只能在运行时确定。我相信注释中使用的值在编译时必须是可解析的。