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

    package com.kafka.consume;

    import org.apache.kafka.clients.consumer.ConsumerRecord;
    import org.springframework.kafka.annotation.KafkaListener;

    import java.lang.annotation.Annotation;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    import java.util.Map;

    interface A {
    String func1();
    }
    class B implements A {

    @Override
    public String func1() {
    return "f1";
    }
    public String func2(){
    return "f2";
    }
    }
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Foo {
    String value();
    }
    class Bar {
    @Foo("fff")
    private String val;

    @KafkaListener(groupId = "demo", topics = "demo")
    public void listenerDemo(ConsumerRecord<?, ?> record) throws Exception{
    System.out.println("--"+record.value());
    }
    }
    public class Main {
    public static void main(String ...args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException {
    Bar bar = new Bar();
    Field field = Bar.class.getDeclaredField("val");
    Foo foo = field.getAnnotation(Foo.class);
    InvocationHandler h = Proxy.getInvocationHandler(foo);
    Field hField = h.getClass().getDeclaredField("memberValues");
    hField.setAccessible(true);
    Map memberValues = (Map) hField.get(h);
    memberValues.put("value", "ddd");
    String value = foo.value();
    System.out.println(value); // ddd
    ///////
    Bar bar1 = new Bar();
    Method[] method = Bar.class.getDeclaredMethods();
    KafkaListener listener = method[0].getAnnotation(KafkaListener.class);
    InvocationHandler hh = Proxy.getInvocationHandler(listener);
    Field hFields = h.getClass().getDeclaredField("memberValues");
    hFields.setAccessible(true);
    Map memberValuess = (Map) hFields.get(hh);
    memberValuess.put("groupId", "mayunzhenGroup");
    System.out.println(listener.groupId());
    }
    }







    @KafkaListener(groupId = "demo", topics = "testx",containerFactory = "kafkaListenerContainerFactory")
    public void listenerDemo(ConsumerRecord<?, ?> record) throws Exception{
    Method[] method = BSMListener.class.getDeclaredMethods();
    KafkaListener listener = method[0].getAnnotation(KafkaListener.class);
    InvocationHandler hh = Proxy.getInvocationHandler(listener);
    Field hFields = null;
    hFields = hh.getClass().getDeclaredField("memberValues");
    hFields.setAccessible(true);
    Map memberValuess = null;
    memberValuess = (Map) hFields.get(hh);
    System.out.println(method[0].getName()+","+memberValuess.get("groupId")+"----------------------------"+record.value());
    }
  • 相关阅读:
    只有在人生的最低处才能看清这个世界
    深刻理解JavaScript原型链
    常用的正则表达式
    JS容易犯错的this和作用域
    站立会议第二天
    站立会议第一天
    典型用户分析
    第七周学习进度
    第六周学习进度
    最大子数组三
  • 原文地址:https://www.cnblogs.com/yunger/p/15018650.html
Copyright © 2011-2022 走看看