
我应该使用枚举代替吗?
你不能
注释变量必须是编译时常量。枚举和String文字都是,但是您不能创建为String且
@RequestMapping需要String的枚举(并且,如果您的enum具有返回String或String字段的方法,则它不是编译时常量)。由于注释处理有多个回合,因此当常量在另一个类中时它确实起作用。
就是说:是的,我会说使用专用的常量类(对于不同类型的常量,可能是几个)是我会尽可能使用的一种好习惯(只要常量a,它就可以使用注释)在具有注释和b)的同一编译单元内部,在声明中进行初始化(与静态初始化程序块相对)。
这是一个例子:
控制者
@Controller @RequestMapping(value = Mappings.CUSTOMER_PAGE)public class CustomerPageController{ // methods here}常量类
public static final class Mappings{ private Mappings(){} public static final String CUSTOMER_PAGE = "path/to/customer/page" // more constants}以下是一些无法使用的版本:
一个)
@Controller @RequestMapping(value = CUSTOMER_PAGE)public class CustomerPageController{ private static final String CUSTOMER_PAGE = "path/to/customer/page";}这不会编译,因为注释在其注释的类内引用了一个常量。这是行不通的,因为在编译过程中,注释将在其余代码之前在单独的回合中处理,而类需要注释已经被处理才能进行编译(即注释和常量之间存在特定的依赖关系)
b)
public static final class Mappings{ private Mappings(){} public static final String CUSTOMER_PAGE; static{ CUSTOMER_PAGE = "path/to/customer/page" } // more constants}尽管这是一个静态的final字段,但它不是编译时常量,因此不能用作注释参数