zoukankan      html  css  js  c++  java
  • java 动态修改注解值

    注解部分
    @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD }) @Documented
    public @interface Cacheable { public enum KeyMode { CACHEKEY, BASIC, ALL; } public String prefix() default ""; public String key() default ""; public KeyMode keyMode() default KeyMode.BASIC; public int expire() default 1800; }
       修改注解内部值
    @Cacheable(keyMode = KeyMode.ALL) public Object cached(final ProceedingJoinPoint pjp) throws Throwable { Class<?> cls = CacheProxyBiz.class; Method method = cls.getMethod("cached", ProceedingJoinPoint.class); Cacheable cacheable = method.getAnnotation(Cacheable.class);//获取注解 System.out.println("修改前....."); System.out.println("模式" + cacheable.keyMode() + " 时长" + cacheable.expire()); InvocationHandler invocationHandler = Proxy.getInvocationHandler(cacheable); Field value = invocationHandler.getClass().getDeclaredField("memberValues"); value.setAccessible(true); Map<String, Object> memberValues = (Map<String, Object>) value.get(invocationHandler); memberValues.put("expire", 50); System.out.println("修改后....."); System.out.println("模式" + cacheable.keyMode() + " 时长" + cacheable.expire()); return ""; }

     

  • 相关阅读:
    竞态与死锁
    Java-核心技术-面试整理
    接口(工厂模式&代理模式)
    switch case实现两个数的算术运算
    继承和多态举例
    字符串的逆序输出
    引用传递&值传递
    递归的使用
    构造方法的重载
    给定数组,去掉0元素后将剩下的元素赋给新的数组
  • 原文地址:https://www.cnblogs.com/wangnanhui/p/10334027.html
Copyright © 2011-2022 走看看