Java 8 可重复的自定义注解 [英] Java 8 repeatable custom annotations

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

问题描述

我了解 Java 中的基本注释,例如 @Override 等.

注解只是元数据,不包含任何业务逻辑.

我正在查看来自 Oracle 文档 页面的重复注释了解 Java 8 新功能.

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

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

声明一个可重复的注解类型

<块引用>

注解类型必须用@Repeatable 元注解标记.下面的例子定义了一个自定义的@Schedule 可重复注解类型:

import java.lang.annotation.Repeatable;@Repeatable(Schedules.class)公共@interface 时间表{String dayOfMonth() 默认第一个";String dayOfWeek() 默认星期一";int hour() 默认 12;}

声明包含的注解类型

<块引用>

包含的注解类型必须有一个数组类型的值元素.数组类型的组件类型必须是可重复的注解类型.包含注解类型的 Schedules 声明如下:

public @interface Schedules {时间表[]值();}

我不了解@Schedules 注释的用法和用法.下面的方法现在如何工作?.

public void doPeriodicCleanup() { ... }

提前致谢.

解决方案

在 Java 8 之前,给定的注解只能在方法(或类、字段等)上设置一次.因此,如果您希望能够在单个方法上设置两个不同的时间表,则必须定义一个包装"注释,例如 Schedules,其中包含一个 Schedule 数组注释.

因此,开发人员必须执行以下操作:

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

这不是很容易阅读,并且 Schedules 注释除了包含多个 Schedule 注释之外没有任何其他用途.

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

注释方法

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

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

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

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

I am going through repeating annotations from Oracle documentation page to understand Java 8 new feature.

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() { ... }

Declare a Repeatable Annotation Type

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;
}

Declare the Containing Annotation Type

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();
}

I did not understand use and usage of @Schedules annotation. How does it work for below method now? .

public void doPeriodicCleanup() { ... }

Thanks in advance.

解决方案

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")
})

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

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")

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天全站免登陆