zoukankan      html  css  js  c++  java
  • java注解

    下图转自:http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html

    下面实现一个自定义注解:

    转自:http://www.cnblogs.com/whoislcj/p/5671622.html

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface ReqType {
    
        enum ReqTypeEnum {
            GET, POST,DELETE,PUT
        }
    
        ReqTypeEnum reqType() default ReqTypeEnum.GET;
    
    }
    @Documented
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface ReqUrl {
        String value() default "";
    }
    @Documented
    @Target(ElementType.PARAMETER)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface ReqParam {
        String value() default "";
    }
    public interface IReqService {
    
        @ReqType(reqType = ReqType.ReqTypeEnum.POST)
        @ReqUrl("www.baidu.com")
        String login(@ReqParam("userId") String userId, @ReqParam("pwd") String pwd);
    }
    public class Test {
        public static void main(String[] args) {
            IReqService service = create(ReqServiceImpl.class);
            String result = service.login("bai", "123456");
            System.out.println(result);
        }
    
        private static <T> T create(final Class<T> service) {
            return (T) Proxy.newProxyInstance(
                    service.getClassLoader(),
                    new Class<?>[]{service},
                    new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            ReqType reqType = method.getAnnotation(ReqType.class);
                            ReqUrl reqUrl = method.getAnnotation(ReqUrl.class);
                            System.out.println("reqType:" + reqType.reqType());
                            System.out.println("reqUrl:" + reqUrl.value());
    
                            Type[] parameterTypes = method.getGenericParameterTypes();
                            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
                            for (int i = 0; i < parameterAnnotations.length; i++) {
                                Annotation[] parameterAnnotation = parameterAnnotations[i];
                                if (parameterAnnotation != null) {
                                    ReqParam reqParam = (ReqParam)parameterAnnotation[0];
                                    System.out.println("reqParam->" + reqParam.value() + "==" + args[i]);
                                }
                            }
    
                            //下面就可以执行相应的网络请求获取结果 返回结果
                            String result = "success";//这里模拟一个结果
    
                            return result;
    
                        }
                    }
    
            );
        }
    }
  • 相关阅读:
    Android数据存储之File
    Openfiler使用说明
    nginx的upstream目前支持5种方式的分配
    centOS 6.5关闭防火墙步骤
    yum下载文件的存放位置
    ubuntu如何开启root,如何启用Ubuntu中root帐号
    ubuntu下的apt-get内网本地源的搭建
    [转]StarWind模拟iSCSI设备
    ORACLE删除某用户下所有对象
    [转]oracle导入提示“IMP-00010:不是有效的导出文件,头部验证失败”的解决方案
  • 原文地址:https://www.cnblogs.com/paulbai/p/6443941.html
Copyright © 2011-2022 走看看