自定义注解在拦截器中的应用

java自定义注解
Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。

在自定义注解拦截器的开发过程中
1、@Retention(RetentionPolicy.RUNTIME)
指定该注解可以被反射获取到
2、@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR })
指定该注解的应用的范围区间
表示支持注解的程序元素的种类,一些可能的值有TYPE, METHOD, CONSTRUCTOR, FIELD等等。如果Target元注解不存在,那么该注解就可以使用在任何程序元素之上。
3、@Inherited
表示一个注解类型会被自动继承,如果用户在类声明的时候查询注解类型,同时类声明中也没有这个类型的注解,那么注解类型会自动查询该类的父类,这个过程将会不停地重复,直到该类型的注解被找到为止,或是到达类结构的顶层(Object)。
注:(cglib动态代理必须要有该注释)
4、@Documented 表示使用该注解的元素应被javadoc或类似工具文档化,它应用于类型声明,类型声明的注解会影响客户端对注解元素的使用。如果一个类型声明添加了Documented注解,那么它的注解会成为被注解元素的公共API的一部分。

默认值
方法后面添加default 值;
无默认值的为必填参数

反射获取
注解配置

package customerCase.copy;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR}) 
@Retention(RetentionPolicy.RUNTIME)
public @interface PermissionInfo {
    PermissionCheckEnum check() default PermissionCheckEnum.CID;
}
@PermissionInfo
public class Bean {
    @PermissionInfo
    public String say(){
        return "say";
    }
}
package customerCase.copy;

import java.lang.reflect.Method;

public class AnnoationTest {
    public static void main(String[] args) {
        String className="customerCase.copy.Bean";
        Class<?> bean = null;
        try {
            bean= Class.forName(className);
            if(bean.equals(Class.class)){
                System.out.println("zhangsanliosi");
            }else if(bean.equals(Bean.class)){
                System.out.println("222222222");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } 
        if (bean != null) {
             PermissionInfo  a = bean.getAnnotation(PermissionInfo.class);
             bean.getClass().getAnnotations();
             Bean.class.getAnnotations();
             if(a!=null){
                 PermissionCheckEnum value = a.check();
                 System.out.println(value);
             }
             Method [] methods=bean.getMethods();
             for (Method method : methods) {
                 System.out.println(method.getName());
                 if("say".equals(method.getName())){
                     System.out.println("-----------------");
                 }
                 PermissionInfo  a11=   method.getAnnotation(PermissionInfo.class);
                 if(a11!=null){
                     PermissionCheckEnum value = a11.check();
                     System.out.println(value);
                 }

            }

        }
    }
}

注解拦截器的使用一般都采用AOP invoke实现
具体代码没有贴出来,上面的是拦截器内部注解处理的核心

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值