Java:自訂註解(Annotation)

來源:互聯網
上載者:User

標籤:extends   .class   typeof   需要   tps   ping   false   ase   etc   

 在網上找了很多資料也有寫的比較好的,但是總有有一點半點鐘的細節沒有寫出來,在這裡自己總結下使用。

 使用Java的自訂註解,首先個人需要瞭解下Java為我們提供的元註解和相關定義註解的文法。(這個我在網上選擇了一篇詳細的介紹連結在文章最底層)

 

1、首先自訂我們需要的註解

package com.plat;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** @Retention(RetentionPolicy.SOURCE)* 這個註解的意思是讓MyAnnotation註解只在java源檔案中存在,編譯成.class檔案後註解就不存在了* @Retention(RetentionPolicy.CLASS)* 這個註解的意思是讓MyAnnotation註解在java源檔案(.java檔案)中存在,編譯成.class檔案後註解也還存在,* 被MyAnnotation註解類標識的類被類載入器載入到記憶體中後MyAnnotation註解就不存在了*//** 這裡是在註解類MyAnnotation上使用另一個註解類,這裡的Retention稱為元註解。* Retention註解括弧中的"RetentionPolicy.RUNTIME"意思是讓MyAnnotation這個註解的生命週期一直程式運行時都存在*///Target註解決定MyAnnotation註解可以加在哪些成分上,如加在類身上,或者屬性身上,或者方法身上等成分/** * @author jwkang *是否需要判斷許可權,預設為true需要判斷許可權,設定為false的情況下不判斷許可權 */@Documented@Inherited@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public @interface PlatPermission {       boolean validate() default true;}

2、將自訂註解標識在不需要判斷許可權的方法上

    @PlatPermission(validate=false)    @RequestMapping(value = "/getSelect", method = {RequestMethod.POST})    @ResponseBody    public BaseOutModel GetSelect(String selectType) {        BaseOutModel result = new BaseOutModel();        LinkedHashMap<String, String> data = new LinkedHashMap<String, String>();        try {                        if(!TypeOfEnum.contains(selectType))            {                result.setResult(false);                result.setErrorMessage("未找到對應資訊");                return result;            }            TypeOfEnum typeOfEnum = TypeOfEnum.get(selectType);            data = EnumHelp.getZEnumDesList(typeOfEnum.getType());            result.setResult(true);            result.setResponse(data);        } catch (Exception e) {            e.printStackTrace();            logger.error("operateEmail err", e.toString());            result.setResult(false);            result.setErrorMessage("系統異常!請重試...");            return result;        }        return result;    }

3、進行許可權的管控

    jar包的引用:

import org.springframework.web.method.HandlerMethod;

   許可權的控制,註解讀取

public class PlatHandlerInterceptorAdapter extends HandlerInterceptorAdapter {    private static final ILog logger = LogManager.getLogger(PlatHandlerInterceptorAdapter.class);    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)            throws Exception {        //處理Permission Annotation,實現方法級許可權控制         //HandlerMethod 需要對應Jar包的位置,否則會一直為false        if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {            PlatPermission permission = ((HandlerMethod) handler).getMethodAnnotation(PlatPermission.class);//開始使用註解            if (permission == null || permission.validate()) {  //註解標識需要判斷許可權                // 許可權判斷,沒有許可權則跳轉至無許可權頁面,有許可權則走正常流程                XXXXX            }        }                return super.preHandle(request, response, handler);    } }

4、完成,整個一套的註解使用

 

 Java註解:http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

 

Java:自訂註解(Annotation)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.