SpringBoot注解之:@Target和@Retention

SpringBoot注解之:@Target和@Retention

@Target

先来看一下源码,发现只有一个参数ElementType[] value()

package java.lang.annotation;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}

我们进入ElementType的源码

package java.lang.annotation;
public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Formal parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}

@Target(ElementType.TYPE)——接口、类、枚举、注解
@Target(ElementType.FIELD)——字段、枚举的常量
@Target(ElementType.METHOD)——方法
@Target(ElementType.PARAMETER)——方法参数
@Target(ElementType.CONSTRUCTOR) ——构造函数
@Target(ElementType.LOCAL_VARIABLE)——局部变量
@Target(ElementType.ANNOTATION_TYPE)——注解
@Target(ElementType.PACKAGE)——包

@Retention

老规矩,还是先来看一下源码,发现也是只有一个参数RetentionPolicy

package java.lang.annotation;

/**
 * Indicates how long annotations with the annotated type are to
 * be retained.  If no Retention annotation is present on
 * an annotation type declaration, the retention policy defaults to
 * {@code RetentionPolicy.CLASS}.
 *
 * <p>A Retention meta-annotation has effect only if the
 * meta-annotated type is used directly for annotation.  It has no
 * effect if the meta-annotated type is used as a member type in
 * another annotation type.
 *
 * @author  Joshua Bloch
 * @since 1.5
 * @jls 9.6.3.2 @Retention
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}

我们进入到这个RetentionPolicy里看一下,发现有三个值

package java.lang.annotation;

/**
 * Annotation retention policy.  The constants of this enumerated type
 * describe the various policies for retaining annotations.  They are used
 * in conjunction with the {@link Retention} meta-annotation type to specify
 * how long annotations are to be retained.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

RetentionPolicy.SOURCE:这种类型的Annotations只在源代码级别保留,编译时就会被忽略,在class字节码文件中不包含。

RetentionPolicy.CLASS:这种类型的Annotations编译时被保留,默认的保留策略,在class文件中存在,但JVM将会忽略,运行时无法获得。

RetentionPolicy.RUNTIME:这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建消费者接口 ```java public interface Consumer<T> { void consume(T message); } ``` 2. 创建不同消费者类 ```java @Component public class TopicAConsumer implements Consumer<String> { @Override public void consume(String message) { System.out.println("TopicAConsumer consume: " + message); } } @Component public class TopicBConsumer implements Consumer<String> { @Override public void consume(String message) { System.out.println("TopicBConsumer consume: " + message); } } @Component public class TopicCConsumer implements Consumer<Integer> { @Override public void consume(Integer message) { System.out.println("TopicCConsumer consume: " + message); } } ``` 3. 创建消费者配置类 ```java @Configuration public class ConsumerConfig { private final Map<String, Consumer<?>> consumers = new ConcurrentHashMap<>(); @Autowired private List<Consumer<?>> allConsumers; @PostConstruct public void init() { allConsumers.forEach(consumer -> { Topic topic = consumer.getClass().getAnnotation(Topic.class); if (topic != null) { String[] topics = topic.value(); for (String t : topics) { consumers.put(t, consumer); } } }); } @Bean public ConsumerFactory consumerFactory() { return new DefaultConsumerFactory(consumers); } @Bean public KafkaListenerAnnotationBeanPostProcessor kafkaListenerAnnotationBeanPostProcessor() { return new KafkaListenerAnnotationBeanPostProcessor(consumerFactory()); } } ``` 4. 创建消费者工厂类 ```java public interface ConsumerFactory { Consumer<?> getConsumer(String topic); } public class DefaultConsumerFactory implements ConsumerFactory { private final Map<String, Consumer<?>> consumers; public DefaultConsumerFactory(Map<String, Consumer<?>> consumers) { this.consumers = consumers; } @Override public Consumer<?> getConsumer(String topic) { return consumers.get(topic); } } ``` 5. 创建注解 ```java @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Topic { String[] value(); } ``` 6. 创建测试类 ```java @Component public class KafkaProducer { @Autowired private KafkaTemplate<String, Object> kafkaTemplate; public void send(String topic, Object message) { kafkaTemplate.send(topic, message); } } @SpringBootApplication public class Application implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Autowired private KafkaProducer kafkaProducer; @Override public void run(String... args) throws Exception { kafkaProducer.send("topicA", "hello topicA"); kafkaProducer.send("topicB", "hello topicB"); kafkaProducer.send("topicC", 123); } } ``` 7. 测试结果 ``` TopicAConsumer consume: hello topicA TopicBConsumer consume: hello topicB TopicCConsumer consume: 123 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值