Return to site

JAVA ANNOTATIONS: @Repeatable

There are some situations where you want to apply the same annotation to a declaration or type use. As of the Java SE 8 release, repeating annotations enable you to do this.

· java

Define

  • The annotation type java.lang.annotation.Repeatable is used to indicate that the annotation type whose declaration it (meta-)annotates is repeatable. 
  • The value of @Repeatable indicates the containing annotation type for the repeatable annotation type.

Snippet

Purpose

Set a timer to run a method, doPeriodicCleanup, on the last day of the month and on every Friday at 11:00 p.m.

Use

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

Thanks to

import java.lang.annotation.Repeatable;

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

And thanks to

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