• 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());
    }
  • 相关阅读:
    缩水版遗传算法 学习笔记
    算法导论 二项堆
    Linux系统编程(6)——文件系统
    Linux系统编程(5)——文件与IO之mmap函数
    Linux系统编程(4)——文件与IO之ioctl函数
    Linux系统编程(3)——文件与IO之fcntl函数
    Linux系统编程(2)——文件与IO之系统调用与文件IO操作
    Linux系统编程(1)——文件与I/O之C标准I/O函数与系统调用I/O
    C语言的本质(38)——makefile之变量
    C语言的本质(37)——makefile之隐含规则和模式规则
  • 原文地址:https://www.cnblogs.com/yunger/p/15018650.html
Copyright © 2020-2023  润新知