One, what is duplicate annotation
Allow multiple use of the same annotation on the same declaration type (class, property, or method)
Simple example of second
Java 8 also has a solution for reusing annotations, but not very readable, such as the following code:
Copy Code code as follows:
Public @interface Authority {
String role ();
}
Public @interface Authorities {
Authority[] Value ();
}
public class Repeatannotationuseoldversion {
@Authorities ({@Authority (role= "Admin"), @Authority (role= "Manager")})
public void dosomething () {
}
}
By another annotation to store duplicate annotations, and when using the storage annotation authorities to extend the duplicate annotations, let's take a look at the practices in Java 8:
Copy Code code as follows:
@Repeatable (Authorities.class)
Public @interface Authority {
String role ();
}
Public @interface Authorities {
Authority[] Value ();
}
public class Repeatannotationusenewversion {
@Authority (role= "Admin")
@Authority (role= "Manager")
public void DoSomething () {}
}
The difference is that when creating a duplicate annotation authority, add @repeatable, point to the storage annotation authorities, and use the authority annotation directly when you use it. From the above example, the Java 8 approach is more suitable for conventional thinking, a little more readable
Third, summary
JEP120 Not much content, is a small feature, just to improve the readability of the code. The Java 8 has made 2 improvements to annotations (Jep 104,jep120) and believes that annotations will be used more frequently than before.