java-8可重复的自定义注释 [英] java-8 repeatable custom annotations

查看:162
本文介绍了java-8可重复的自定义注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道java中的基本注释,如 @Override 等。

I know about basic annotations in java like @Override etc.

Annotations are only metadata and they do not contain any business logic.

我正在重复oracle的注释文档页面,了解 java-8 新功能。

I am going through repeating annotations from oracle documentation page to understand java-8 new feature.

例如,您正在编写代码以使用计时器服务,该服务使您能够在给定时间或某个计划运行方法,类似于UNIX cron服务。现在你要设置一个计时器来运行一个方法, doPeriodicCleanup ,在每个月的最后一天和每个星期五的晚上11点。要设置要运行的计时器,请创建 @Schedule 注释并将其应用于doPeriodicCleanup方法两次。

For example, you are writing code to use a timer service that enables you to run a method at a given time or on a certain schedule, similar to the UNIX cron service. Now you want to set a timer to run a method, doPeriodicCleanup, on the last day of the month and on every Friday at 11:00 p.m. To set the timer to run, create an @Schedule annotation and apply it twice to the doPeriodicCleanup method.

@Schedule(dayOfMonth="last")
@Schedule(dayOfWeek="Fri", hour="23")
public void doPeriodicCleanup() { ... }

声明可重复的注释类型


注释类型必须用@Repeatable元注释标记。以下示例定义自定义@Schedule可重复注释类型:

The annotation type must be marked with the @Repeatable meta-annotation. The following example defines a custom @Schedule repeatable annotation type:



import java.lang.annotation.Repeatable;

@Repeatable(Schedules.class)
public @interface Schedule {
  String dayOfMonth() default "first";
  String dayOfWeek() default "Mon";
  int hour() default 12;
}

声明包含注释类型


包含注释类型必须具有带数组类型的value元素。数组类型的组件类型必须是可重复的注释类型。包含注释类型的Schedules的声明如下:

The containing annotation type must have a value element with an array type. The component type of the array type must be the repeatable annotation type. The declaration for the Schedules containing annotation type is the following:



public @interface Schedules {
    Schedule[] value();
}

我不明白@Schedules注释的使用和使用。它现在如何适用于以下方法?

public void doPeriodicCleanup() { ... }

提前致谢。

推荐答案

在Java 8之前,给定的注释可以仅在方法(或类或字段等)上设置一次。因此,如果您希望能够在单个方法上设置两个不同的计划,则必须定义一个包装注释,例如计划,其中包含<$的数组c $ c>安排注释。

Before Java 8, a given annotation could only be set once on a method (or class, or field, etc.). So, if you wanted to be able to set two different schedules on a single method, you had to define a "wrapping" annotation such as Schedules, containing an array of Schedule annotions.

开发人员因此不得不这样做:

The developer thus had to do something like this:

@Schedules(value = {
    @Schedule(dayOfMonth="last"),
    @Schedule(dayOfWeek="Fri", hour="23")
})

这不是非常易读,而且时间表注释不具备除了包含几个Schedule注释之外还有其他目的。

This is not very readable, and the Schedules annotation doesn't have any purpose other than containing several Schedule annotations.

为了减少样板,但保持注释API相同,现在允许使用

To reduce the boilerplate, but keep the annotations API identical, it's now allowed to simply annotate the method with

@Schedule(dayOfMonth="last"),
@Schedule(dayOfWeek="Fri", hour="23")

通过将Schedule声明为可重复并指定其包装注释。但它只是语法糖,导致与前面的代码相同:在运行时,该方法被视为使用包含两个Schedule的单个Schedules注释进行注释。编译器将第二段代码转换为第一段代码。

by declaring Schedule as repeatable and specifying its "wrapping" annotation. But it's just syntax sugar, that results in the same thing as the previous code: the method, at runtime, is seen as being annotated with a single Schedules annotation containing two Schedule. The compiler transforms the second piece of code into the first one.

这篇关于java-8可重复的自定义注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆