[英]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.