应该将映射值声明为常量还是枚举? [英] Should mapping value be declared in a constant or as an enum?

查看:134
本文介绍了应该将映射值声明为常量还是枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这散落在代码库中:

I see this scattered throughout code base:

@RequestMapping(value = "myValue")

我更喜欢使用这样的东西:

I would prefer to use something like this:

@RequestMapping(value = Constants.myValue)

@RequestMapping 中的实际String值而不是常量。
但是这是很好的代码实践吗?我应该使用枚举吗?
我可能需要在代码库中的其他地方使用 Constants.myValue

It seems to break DRY using the actual String value within @RequestMapping instead of constant. But is this good code practice? Should I use an enum instead? I may need to use Constants.myValue elsewhere in the code base.

推荐答案


我应该使用枚举吗?

Should I use an enum instead?

你不能。注释变量必须是编译时常数。枚举和字符串两者都是,但是您不能创建一个字符串的枚举,并且 @RequestMapping 需要一个String(如果你的枚举有一个方法返回一个String或一个String字段,这不是一个编译时常数)。由于有多轮注解处理,当常数在另一个类中时,它的工作正常。

You can't. Annotation variables must be compile-time constants. Enums and String literals both are, but you can't create an enum that is a String and @RequestMapping needs a String (and if your enum has a method that returns a String or a String field, that's not a compile-time constant). Since there are multiple rounds of annotation processing it does work when the constant is in another class.

说:是的,我会说使用一个专用的常量类几个,对于不同类型的常量)是一个很好的做法,我使用每当我可以(并且它使用注释,只要常量a)没有在同一个编辑单元内定义,并且b)被初始化为声明(与静态初始化程序块相反))

That said: yes, I'd say using a dedicated constants class (perhaps several, for different types of constants) is a good practice that I use whenever I can (and it works with annotations as long as the constant a) is not defined inside the same compilation unit that has the annotation and b) is initialized in the declaration (as opposed to a static initializer block)).

以下是一个示例:

控制器

@Controller @RequestMapping(value = Mappings.CUSTOMER_PAGE)
public class CustomerPageController{
    // methods here
}

常量类 p>

Constants class

public static final class Mappings{
    private Mappings(){}
    public static final String CUSTOMER_PAGE = "path/to/customer/page"
    // more constants
}

这里有一些版本不起作用:

And here are some versions that won't work:

a)

@Controller @RequestMapping(value = CUSTOMER_PAGE)
public class CustomerPageController{
    private static final String CUSTOMER_PAGE = "path/to/customer/page";
}

这将不会编译,因为注释引用了它注释的类中的常量。这不能工作,因为在编译期间,注释在代码的其余部分之前的单独循环中被处理,而类需要已经被编译的注释(即,注释和常量之间有一个细微的依赖关系)。

This won't compile because the annotation references a constant inside the class it annotates. This can't work because during compilation, annotations are processed in a separate round before the rest of the code, while the class needs the annotation to already be processed for compilation (i.e. there's a cicular dependency between annotation and constant)

b)

public static final class Mappings{
    private Mappings(){}
    public static final String CUSTOMER_PAGE;
    static{
        CUSTOMER_PAGE = "path/to/customer/page"
    }

    // more constants
}

尽管这是静态的final字段,但它不是编译时常量,因此不能用作注释参数

Although this is a static final field, it is not a compile-time constant, hence it can't be used as an annotation parameter

这篇关于应该将映射值声明为常量还是枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆