zoukankan      html  css  js  c++  java
  • java注解的使用

      注解(Annotation)相当于一种标记,在程序中加入注解就等于为程序打上某种标记,没有加,则等于没有任何标记,以后,javac编译器、开发工具和其他程序可以通过反射来了解你的类及各种元素上有无何种标记,看你的程序有什么标记,就去干相应的事,标记可以加在包、类,属性、方法,方法的参数以及局部变量上。

    1.自定义注解的两种实现方法

    第一种 反射

    public class MyRateTest {
        private Method method;
        private MyRate myRate;
        private Annotation ann;
    
    
        @MyRate
        public void test(){
            System.out.println("this is method");
        }
    
        @Before
        public void init(){
            method = ReflectionUtils.findMethod(MyRateTest.class,"test");
            myRate = method.getDeclaredAnnotation(MyRate.class);
            ann = method.getDeclaredAnnotation(MyRate.class);
            System.out.println("@before init ~~");
            System.out.println(ann == myRate);
        }
    
        @Test
        public void test1() throws Exception{
            long time = System.currentTimeMillis();
    
            Method[] methods = new MyRateTest().getClass().getDeclaredMethods();for(Method method : methods){if(method.getAnnotation(MyRate.class) != null){
            //业务操作 System.out.println(method.getAnnotation(MyRate.
    class)); method.invoke(new MyRateTest(),null); } } System.out.println("time "+ (System.currentTimeMillis() - time)); } @After public void after(){ System.out.println("this is after"); } @After public void after2(){ System.out.println("this is after2"); } }

    第二种 spring 切面

    @Pointcut("@within(com.xxx.RateLimit)")
        public void pointcut() {
        }
    
     @Around("pointcut()")
        public Object around(JoinPoint joinPoint) throws Throwable {
            MethodInvocationProceedingJoinPoint methodJoinPoint = (MethodInvocationProceedingJoinPoint) joinPoint;
            Method method = ((MethodSignature) methodJoinPoint.getSignature()).getMethod();
            String methodName = method.getName();
            String className = method.getDeclaringClass().getName();
            String argClassNames = JSON.toJSONString(getMethodParameterClassNames(method));
            String keyField = className + "";
            Config config = null;
            try {
                MethodTarget target;
                if ((config = allConfigs.get(keyField)) != null) {
                    target = config.getTarget();
                    int argNo = target.getArgNo();
                    String fieldName = target.getFieldName();
                    String fieldValue = getFieldValue(methodJoinPoint, argNo, fieldName);
                    String key = className + methodName + argClassNames + argNo + fieldName + fieldValue;
                    config = fieldConfigs.get(key);
                }
            } catch (Exception e) {
                log.error("get config error", e);
            }
            doGetRateLimit(config);
            try {
                return methodJoinPoint.proceed();
            } finally {
                doReleaseRateLimit(config);
            }
        }

    参考 : https://www.cnblogs.com/xdp-gacl/p/3622275.html

  • 相关阅读:
    TSP-UK49687
    维度建模的基本原则
    差分约束系统
    随机过程初步
    随机过程——维纳过程
    Xilinx FPGA复位信号设计
    10 Row Abacus
    Python
    FX2LP与FPGA的简单批量回环
    DFT公式的一个简单示例
  • 原文地址:https://www.cnblogs.com/number7/p/9812913.html
Copyright © 2011-2022 走看看