Should mapping value be declared in a constant or as an enum?

后端 未结 1 1798
有刺的猬
有刺的猬 2021-01-02 07:01

I see this scattered throughout code base:

@RequestMapping(value = \"myValue\")

I would prefer to use something like this:



        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 07:23

    Should I use an enum instead?

    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.

    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)).

    Here's an example:

    Controller

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

    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 circular 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
    }
    

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

    0 讨论(0)
提交回复
热议问题