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());
    }
  • 相关阅读:
    百度地图API显示多个标注点带百度样式信息检索窗口的代码
    百度地图API显示多个标注点并添加百度样式检索窗口
    log4j.xml 为什么要使用SLF4J而不是Log4J
    C++编程学习52个经典网站 强力推荐
    一些User-Agent
    outlook 2003配置连接exchange server 2010报错——无法完成此操作。 与 Microsoft Exchange Server 的连接不可用。 Outlook 必须联机或连接才可完成该操作
    postfix+dovecot邮箱服务器
    Java学习之InputStream中read()与read(byte[] b)
    Java Scoket之java.io.EOFException解决方案
    java.util.zip.GZIPInputStream.readUByte,Not in GZIP format错误处理
  • 原文地址:https://www.cnblogs.com/yunger/p/15018650.html
Copyright © 2011-2022 走看看