久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='8y0Bh'></bdo><ul id='8y0Bh'></ul>
    <i id='8y0Bh'><tr id='8y0Bh'><dt id='8y0Bh'><q id='8y0Bh'><span id='8y0Bh'><b id='8y0Bh'><form id='8y0Bh'><ins id='8y0Bh'></ins><ul id='8y0Bh'></ul><sub id='8y0Bh'></sub></form><legend id='8y0Bh'></legend><bdo id='8y0Bh'><pre id='8y0Bh'><center id='8y0Bh'></center></pre></bdo></b><th id='8y0Bh'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='8y0Bh'><tfoot id='8y0Bh'></tfoot><dl id='8y0Bh'><fieldset id='8y0Bh'></fieldset></dl></div>
      <legend id='8y0Bh'><style id='8y0Bh'><dir id='8y0Bh'><q id='8y0Bh'></q></dir></style></legend><tfoot id='8y0Bh'></tfoot>

      <small id='8y0Bh'></small><noframes id='8y0Bh'>

      使用 Enum 類型作為 @RolesAllowed-Annotation 的值參數

      Use Enum type as a value parameter for @RolesAllowed-Annotation(使用 Enum 類型作為 @RolesAllowed-Annotation 的值參數)

      <small id='dN0t7'></small><noframes id='dN0t7'>

      <tfoot id='dN0t7'></tfoot>

            • <legend id='dN0t7'><style id='dN0t7'><dir id='dN0t7'><q id='dN0t7'></q></dir></style></legend>
                <tbody id='dN0t7'></tbody>
              <i id='dN0t7'><tr id='dN0t7'><dt id='dN0t7'><q id='dN0t7'><span id='dN0t7'><b id='dN0t7'><form id='dN0t7'><ins id='dN0t7'></ins><ul id='dN0t7'></ul><sub id='dN0t7'></sub></form><legend id='dN0t7'></legend><bdo id='dN0t7'><pre id='dN0t7'><center id='dN0t7'></center></pre></bdo></b><th id='dN0t7'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='dN0t7'><tfoot id='dN0t7'></tfoot><dl id='dN0t7'><fieldset id='dN0t7'></fieldset></dl></div>
                <bdo id='dN0t7'></bdo><ul id='dN0t7'></ul>
              • 本文介紹了使用 Enum 類型作為 @RolesAllowed-Annotation 的值參數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在開發一個 Java 企業應用程序,目前正在做 Java EE 安全工作,以限制特定用戶對特定功能的訪問.我配置了應用程序服務器和所有內容,現在我使用 RolesAllowed-annotation 來保護方法:

                I'm developing a Java enterprise application, currently doing Java EE security stuff to restrict access for particular functions to specific users. I configured the application server and everything, and now I'm using the RolesAllowed-annotation to secure the methods:

                @Documented
                @Retention (RUNTIME)
                @Target({TYPE, METHOD})
                public @interface RolesAllowed {
                    String[] value();
                }
                

                當我像這樣使用注釋時,它工作正常:

                When I use the annotation like this, it works fine:

                @RolesAllowed("STUDENT")
                public void update(User p) { ... }
                

                但這不是我想要的,因為我必須在這里使用字符串,重構變得困難,并且可能會發生拼寫錯誤.因此,我不想使用字符串,而是使用枚舉值作為此注釋的參數.枚舉看起來像這樣:

                But this is not what I want, as I have to use a String here, refactoring becomes hard, and typos can happen. So instead of using a String, I would like to use an Enum value as a parameter for this annotation. The Enum looks like this:

                public enum RoleType {
                    STUDENT("STUDENT"),
                    TEACHER("TEACHER"),
                    DEANERY("DEANERY");
                
                    private final String label;
                
                    private RoleType(String label) {
                        this.label = label;
                    }
                
                    public String toString() {
                        return this.label;
                    }
                }
                

                所以我嘗試使用 Enum 作為這樣的參數:

                So I tried to use the Enum as a parameter like this:

                @RolesAllowed(RoleType.DEANERY.name())
                public void update(User p) { ... }
                

                然后我得到以下編譯器錯誤,盡管 Enum.name 只返回一個字符串(它始終是常量,不是嗎?).

                But then I get the following compiler error, although Enum.name just returns a String (which is always constant, isn't it?).

                注解屬性RolesAllowed.value的值必須是常量表達式`

                The value for annotation attribute RolesAllowed.value must be a constant expression`

                接下來我嘗試在我的枚舉中添加一個額外的最終字符串:

                The next thing I tried was to add an additional final String to my Enum:

                public enum RoleType {
                    ...
                    public static final String STUDENT_ROLE = STUDENT.toString();
                    ...
                }
                

                但這也不能作為參數起作用,導致同樣的編譯器錯誤:

                But this also doesn't work as a parameter, resulting in the same compiler error:

                // The value for annotation attribute RolesAllowed.value must be a constant expression
                @RolesAllowed(RoleType.STUDENT_ROLE)
                

                如何實現我想要的行為?我什至實現了自己的攔截器來使用自己的注釋,這很漂亮,但是對于這樣的小問題,代碼行數太多了.

                How can I achieve the behavior I want? I even implemented my own interceptor to use my own annotations, which is beautiful, but far too much lines of codes for a little problem like this.

                免責聲明

                這個問題最初是一個 Scala 問題.我發現 Scala 不是問題的根源,所以我首先嘗試在 Java 中執行此操作.

                This question was originally a Scala question. I found out that Scala is not the source of the problem, so I first try to do this in Java.

                推薦答案

                我認為您使用枚舉的方法行不通.我發現如果我將最后一個示例中的 STUDENT_ROLE 字段更改為常量字符串,而不是表達式,編譯器錯誤就會消失:

                I don't think your approach of using enums is going to work. I found that the compiler error went away if I changed the STUDENT_ROLE field in your final example to a constant string, as opposed to an expression:

                public enum RoleType { 
                  ...
                  public static final String STUDENT_ROLE = "STUDENT";
                  ...
                }
                

                但是,這意味著枚舉值不會在任何地方使用,因為您將在注釋中使用字符串常量.

                However, this then means that the enum values wouldn't be used anywhere, because you'd be using the string constants in annotations instead.

                在我看來,如果你的 RoleType 類只包含一堆靜態的 final String 常量,你會做得更好.

                It seems to me that you'd be better off if your RoleType class contained nothing more than a bunch of static final String constants.

                要了解您的代碼未編譯的原因,我查看了 Java 語言規范 (JLS).annotations 的 JLS 聲明對于注釋帶有 T 類型的參數和值 V,

                To see why your code wasn't compiling, I had a look into the Java Language Specification (JLS). The JLS for annotations states that for an annotation with a parameter of type T and value V,

                如果T 是原始類型或String,則V 是常量表達式.

                if T is a primitive type or String, V is a constant expression.

                常量表達式包括,其中其他的,

                TypeName 形式的限定名稱.標識符表示常量變量

                Qualified names of the form TypeName . Identifier that refer to constant variables

                并且定義了一個常量變量作為

                原始類型或 String 類型的變量,它是最終的并使用編譯時常量表達式進行初始化

                a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression

                這篇關于使用 Enum 類型作為 @RolesAllowed-Annotation 的值參數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用錯誤)
                Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 語句 - 是“或/“和可能的?)
                Java Replace Character At Specific Position Of String?(Java替換字符串特定位置的字符?)
                What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作數的三元表達式的類型是什么?)
                Read a text file and store every single character occurrence(讀取文本文件并存儲出現的每個字符)
                Why do I need to explicitly cast char primitives on byte and short?(為什么我需要在 byte 和 short 上顯式轉換 char 原語?)
                  <tbody id='v9xsp'></tbody>

                • <legend id='v9xsp'><style id='v9xsp'><dir id='v9xsp'><q id='v9xsp'></q></dir></style></legend>
                      <bdo id='v9xsp'></bdo><ul id='v9xsp'></ul>

                      1. <tfoot id='v9xsp'></tfoot>

                        <small id='v9xsp'></small><noframes id='v9xsp'>

                        <i id='v9xsp'><tr id='v9xsp'><dt id='v9xsp'><q id='v9xsp'><span id='v9xsp'><b id='v9xsp'><form id='v9xsp'><ins id='v9xsp'></ins><ul id='v9xsp'></ul><sub id='v9xsp'></sub></form><legend id='v9xsp'></legend><bdo id='v9xsp'><pre id='v9xsp'><center id='v9xsp'></center></pre></bdo></b><th id='v9xsp'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='v9xsp'><tfoot id='v9xsp'></tfoot><dl id='v9xsp'><fieldset id='v9xsp'></fieldset></dl></div>
                        • 主站蜘蛛池模板: 91欧美精品成人综合在线观看 | 久久国产精品一区二区三区 | 精品福利在线视频 | 一区二区三区四区在线视频 | 99re国产精品 | 欧美一区二区三区 | 激情欧美日韩一区二区 | 五月综合久久 | 超碰97免费在线 | 成人小视频在线观看 | 日本成人中文字幕 | 国产一区二区影院 | 9999国产精品欧美久久久久久 | 日韩国产精品一区二区三区 | 国产日韩欧美综合 | 一区2区| 久久黄色网| 欧美日韩视频在线第一区 | www.亚洲一区二区三区 | 97精品国产97久久久久久免费 | 亚洲福利在线观看 | 97精品国产97久久久久久免费 | 一级看片免费视频囗交动图 | 午夜不卡福利视频 | 国产精品久久久久久久久久 | 97国产精品视频人人做人人爱 | 99热精品久久 | 久久精品久久久久久 | 五月天国产在线 | 色av一区二区三区 | 日韩av一区二区在线观看 | 黄a大片 | 理论片免费在线观看 | 亚洲男人天堂 | 中文字幕亚洲视频 | 亚洲一区影院 | 国产精品国产a | 日韩最新网站 | avhd101在线成人播放 | 国产h在线 | 精品91久久 |