`
QING____
  • 浏览: 2234095 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JAVA中@Repeatable注解

    博客分类:
  • JAVA
 
阅读更多

    @Repeatable注解以及其相关特性,是在JDK 8中提供的,简单而言,我们可以设计一种风格的annotation,可以被多次修饰在符合要求的@Target上,在此前一个类型的注释是不能重复修饰在相同地方。被重复修饰的注解,表示此Target同时具有注解的驱动能力,比如下文的@Scheduled,表示此任务方法分别在各自的时机执行。

 

    对于JAVA开发人员,我们常见的、已经基于@Repeatable的注解,比如:

    1、@ComponentScan --- Spring

    2、@Scheduled --- Spring

 

    以Spring @Scheduled为例,我们简述一下其使用场景:

 

    1、@Scheduled元注解

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {

    //cron表达式
    String value();
}

 

    元注解与普通注解没有太大区别,比如target、集成关系等。唯一需要注意的时,需要使用@Repeatable表示其可以被重复修饰目标上,其中还要指定其容器注解类类型---@Schedules。由此可见,元注解和容器注解,需要互相关联。

 

    2、@Schedules容器注解类

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Schedules {

    Scheduled[] value();
}

 

    1)容器注解类的@Target设计上需要考虑周全,它是否可以像元注解一样单独使用;以上述为例,表明@Schedules也可以被直接使用。

    2)容器类,应该像例子所示,其value方法(默认方法)需要返回其元注解的列表,否则这个容器类设计将不符合规范。

 

    3、解析注解,我们分别使用java反射和spring辅助类实现:

public class TaskRunner {

    @Scheduled("0 0/15 * * * ?")
    @Scheduled("0 0 12 * ?")
    public void task1() {}

    @Schedules({@Scheduled("0 0/15 * * * ?"),
            @Scheduled("0 0 12 * ?")})
    public void task2() {}


    public static void main(String[] args) throws Exception{
        javaStyle();
        System.out.println("-----------");
        springStyle();
    }


    private static void javaStyle() throws Exception{
        Method[] methods = TaskRunner.class.getMethods();
        for (Method method : methods) {
            Schedules schedules = method.getAnnotation(Schedules.class);
            if (schedules == null) {
                continue;
            }
            Scheduled[] values = schedules.value();
            if (values == null) {
                continue;
            }
            for (Scheduled scheduled : values) {
                System.out.println(scheduled.value());
            }
        }
    }

    private static void springStyle() throws Exception {
        Method[] methods = TaskRunner.class.getMethods();
        for (Method method : methods) {
            Set<Scheduled> schedules = AnnotatedElementUtils.getMergedRepeatableAnnotations(
                    method, Scheduled.class, Schedules.class);
            if (schedules == null) {
                continue;
            }
            for (Scheduled scheduled : schedules) {
                System.out.println(scheduled.value());
            }
        }
    }
}

 

    根据规范,@Scheduled元注解,不能与@Schedules容器注解同时修饰在同一个Target,否则会编译错误。

 

分享到:
评论

相关推荐

    java8源码-Annotation_demo:Annotation_demo

    由于无论在Java后台或者Android开发中我们经常遇到注解这个功能机制,例如常用的框架Java后台开发中,Spring、MyBatis等,Android的Dagger2,butterknife等,都是注解框架。今天我们就了解java是如何进行设置注解的...

    Java内功修炼系列:注解(Annotation)

    目录 一 注解的定义 二 如何自定义注解 三 元注解 3.1 @Retention 3.2 @Target 3.3 @Documented 3.4 @Inherited 3.5 @Repeatable 四 Java 预置的注解 4.1 @Deprecated ...五 注解的属性 ...Java 注解是从 Java5 开

    Json全部jar包

    Json解析的Jar包,包括json-lib-2.2.2-jdk15.jar等

    Android代码-java8-tutorial

    Backed by short and simple code samples you'll learn how to use default interface methods, lambda expressions, method references and repeatable annotations. At the end of the article you'll be ...

    Troubleshooting Java Performance

    These steps guide you through a troubleshooting process that is repeatable, that you can apply to any performance problem in a Java application. This technique is especially helpful in 'dark' ...

    浅析MYSQL REPEATABLE-READ隔离级别

    REPEATABLE-READ 即可重复读,set autocommit= 0或者START TRANSACTION状态下select表的内容不会改变。这种隔离级别可能导致读到的东西是已经修改过的。 比如: 回话一中读取一个字段一行a=1 在回话二里这个字段该行...

    java-数据库系统原理.docx

    可重复读(REPEATABLE READ) 保证在同一个事务中多次读取同样数据的结果是一样的。 可串行化(SERIALIZABLE) 强制事务串行执行。 java-数据库系统原理全文共3页,当前为第2页。事务并发访问引起的问题及使用哪种...

    JAIN实现简单点对点聊天软件 sip聊天软件 JAIN-sip聊天软件

    r repeatable builds BUILD SUCCESSFUL Total time: 0 seconds 3.执行命令: java -jar textclient.jar &lt;username&gt; &lt;port&gt; 启动一个客户端 如:java -jar textclient.jar bob 9999 4.再次执行上面的命令启动另外...

    junit4.4, A programmer-oriented testing framework for Java.

    JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.

    General Photonics Highly Repeatable All Solid-State Polarization State

    . Up to 625 kS/s polarization sampling rate . Analog bandwidth up to 1MHz . Real-time Poincaré sphere display . Continuous scan mode . External trigger mode ... Long-term monitoring mod

    WSDD + TOMCAT 一键部署Webservices

    set to false for repeatable builds [javac] Compiling 5 source files to D:\java\wsdd\target\bin [copy] Copying 3 files to D:\java\wsdd\target\bin build.jar: [jar] Building jar: D:\java\wsdd\target\...

    java收银系统源码-Build-Repeatable-Infrastructure-using-Terraform-and-Ansible:

    java收银系统源码在 AWS 上使用 Terraform 和 Ansible 构建可重复的基础设施 许多公司仍在为基本 IT 运营而苦苦挣扎。 他们花费太多时间灭火并手动构建、配置和维护基础设施,导致本可以帮助公司转型的大量资金损失...

    design of accurate and repeatable kinematics couplings.pdf

    本文研究了一个用于在六个自由度调节kinematics coupling结构的装置,挺有意思的设计。

    基于框架的Web开发-用户注册.docx

    CustomerServiceImpl类中,需要更改数据表的方法上(insertNewCustomer方上),加下面事务注解: @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED, readOnly = false) 4

    Spring.html

    repeatableRead:可重复读,可以解决脏读,不可重复读问题 Serializbler:串行化,可以解决所有问题 超时时间: 默认-1(永不超时),事务一直不提交也不回滚的时间 是否只读: 默认false ...

    Java版水果管理系统源码-WhatTheFuck:该死的

    Repeatable read 幻读 事务在插入已经检查过不存在的记录时,发现这些数据已经存在了,之前的检测获取到的数据如同鬼影一般。 Serializable java 类加载机制 重入锁 synchronized 和 Lock的区别 synchronized的实现...

    MySQL数据库:事务隔离级别.pptx

    隔离性强制对事务进行某种程度的隔离,保证应用程序在事务中看到一致的数据。 MySQL提供了下面4种隔离级:序列化(SERIALIZABLE)、可重复读(REPEATABLE READ)、提交读(READ COMMITTED)、未提交读(READ ...

    jQuery表单jquery-repeater.zip

     new Repeater($('.repeatable'), {  addSelector: '.repeater-add', //The css selector for the add button.  removeSelector: '.repeater-remove', //The css selector for the remove ...

Global site tag (gtag.js) - Google Analytics