pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    <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="prnrtt5" 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="bhvv7nv" 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="tjp57fb" id='v9xsp'><tfoot id='v9xsp'></tfoot><dl id='v9xsp'><fieldset id='v9xsp'></fieldset></dl></div>
                        • 主站蜘蛛池模板: 金蝶帐无忧|云代账软件|智能财税软件|会计代账公司专用软件 | 油冷式_微型_TDY电动滚筒_外装_外置式电动滚筒厂家-淄博秉泓机械有限公司 | 剪刃_纵剪机刀片_分条机刀片-南京雷德机械有限公司 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 节流截止放空阀-不锈钢阀门-气动|电动截止阀-鸿华阀门有限公司 | 电加热导热油炉-空气加热器-导热油加热器-翅片电加热管-科安达机械 | 温州在线网| 自动螺旋上料机厂家价格-斗式提升机定制-螺杆绞龙输送机-杰凯上料机 | 三佳互联一站式网站建设服务|网站开发|网站设计|网站搭建服务商 赛默飞Thermo veritiproPCR仪|ProFlex3 x 32PCR系统|Countess3细胞计数仪|371|3111二氧化碳培养箱|Mirco17R|Mirco21R离心机|仟诺生物 | 成都亚克力制品,PVC板,双色板雕刻加工,亚克力门牌,亚克力标牌,水晶字雕刻制作-零贰捌广告 | 大型果蔬切片机-水果冬瓜削皮机-洗菜机切菜机-肇庆市凤翔餐饮设备有限公司 | 上海办公室装修公司_办公室设计_直营办公装修-羚志悦装 | 双菱电缆-广州电缆厂_广州电缆厂有限公司 | 国际高中-国际学校-一站式择校服务-远播国际教育 | 硅胶布|电磁炉垫片|特氟龙胶带-江苏浩天复合材料有限公司 | 玻璃瓶厂家_酱菜瓶厂家_饮料瓶厂家_酒瓶厂家_玻璃杯厂家_徐州东明玻璃制品有限公司 | 立式_复合式_壁挂式智能化电伴热洗眼器-上海达傲洗眼器生产厂家 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 郑州水质检测中心_井水检测_河南废气检测_河南中环嘉创检测 | 流程管理|流程管理软件|企业流程管理|微宏科技-AlphaFlow_流程管理系统软件服务商 | 浇注料-高铝砖耐火砖-郑州凯瑞得窑炉耐火材料有限公司 | 连栋温室大棚建造厂家-智能玻璃温室-薄膜温室_青州市亿诚农业科技 | 雨水收集系统厂家-雨水收集利用-模块雨水收集池-徐州博智环保科技有限公司 | 水质监测站_水质在线分析仪_水质自动监测系统_多参数水质在线监测仪_水质传感器-山东万象环境科技有限公司 | 废气处理设备-工业除尘器-RTO-RCO-蓄热式焚烧炉厂家-江苏天达环保设备有限公司 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 石油/泥浆/不锈钢防腐/砂泵/抽砂泵/砂砾泵/吸砂泵/压滤机泵 - 专业石油环保专用泵厂家 | 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | 盛源真空泵|空压机-浙江盛源空压机制造有限公司-【盛源官网】 | 锡膏喷印机-全自动涂覆机厂家-全自动点胶机-视觉点胶机-深圳市博明智控科技有限公司 | 新型锤式破碎机_新型圆锥式_新型颚式破碎机_反击式打沙机_锤式制砂机_青州建源机械 | 紧急切断阀_气动切断阀_不锈钢阀门_截止阀_球阀_蝶阀_闸阀-上海上兆阀门制造有限公司 | 客服外包专业服务商_客服外包中心_网萌科技 | 派克防爆伺服电机品牌|国产防爆伺服电机|高低温伺服电机|杭州摩森机电科技有限公司 | 瓶盖扭矩测试仪-瓶盖扭力仪-全自动扭矩仪-济南三泉中石单品站 | IHDW_TOSOKU_NEMICON_EHDW系列电子手轮,HC1系列电子手轮-上海莆林电子设备有限公司 | 小区健身器材_户外健身器材_室外健身器材_公园健身路径-沧州浩然体育器材有限公司 | 珠光砂保温板-一体化保温板-有釉面发泡陶瓷保温板-杭州一体化建筑材料 | 磁力轮,磁力联轴器,磁齿轮,钕铁硼磁铁-北京磁运达厂家 | 一体化隔油提升设备-餐饮油水分离器-餐厨垃圾处理设备-隔油池-盐城金球环保产业发展有限公司 | 拖鞋定制厂家-品牌拖鞋代加工厂-振扬实业中国高端拖鞋大型制造商 | 苏州柯瑞德货架-仓库自动化改造解决方案 |