[J2SE 5.0 Topics] Note

xiaoxiao2021-03-06  23

Annotations in I seem to be a significant new feature in J2SE 5.0. The future EJB 3.0 specification will fully utilize this feature to simplify the definition and implementation of the session bean, message-driven bean, and EntityBeans.

Java's annotations are more or less affected by .NET. This is not to say that Java has not been annotated, the previous label, especially @Deprecated, is annotated, only this time update will be annotated to the unprecedented height of Java history. By annotation, we can achieve the original relatively complex advanced features.

First look at an actual example:

/ * * CREATED ON 2004-12-28 * * /

Import java.lang.annotation. *;

/ ** * @Author sean * * / public class myannotation {

@MYANNOTATIONFORMETHODS (INDEX = 1, info = "this is a method to test myannotation.", Developer = "somebody else") public void testmethod1 () {// ...}

@MyEmptyAnnotation @MySingleElementAnnotation ( "For instruction purpose only.") @MyAnnotationForMethods (index = 2, info = "This method is to show multiple annotations.") Public void testMethod2 () {// ...}}

@Target (ElementType.Method) @Interface myannotationformethods {int index (); string info (); string developer () default "sean gao";

@Interface myemptyannotation {}

@Interface mysingleelementannotation {string value ();

In this example, I have defined three annotations: the first MyanNotationFormethods contains three members, which are index, info and developer, respectively, where developer has the default value "Sean Gao", the front @Target (ElementType.Method) Note from this annotation, indicating that the annotation can only be used by the method; the second MyemptyanNotation is an empty annotation, which can be used as a tag, such as the earlier serializable interface; the third MySingleElementanNotation contains a member, Value, this is a hard regulation When there is only one element, it must be used with the name, its type can be a value, string, clas, enum, or a unit of data type described above.

The definition of the annotation is very similar to the interface, but they have considerable differences on the use of the interface. Specific usage refers to the example of me. The annotations are superimposed.

In practical applications, it is currently a more mature thing that some test frameworks, such as the original writer CEDRIC beust written in another article. In this article, I have mentioned that there is a considerable amount of annotations in the future EJB 3.0 specification, let us preview the future stateless session bean to define what will be like: @StateLess Public CLASS Bookshelfmanagerbean {public void addbook (book access) {// business logic goes here ...} public collection getAllBooks () {// business logic goes here ...} // ...}

That's it. We don't even write any interfaces and deployment descriptors, how nice.

More detailed description of the syndrome, refer to here.

转载请注明原文地址:https://www.9cbs.com/read-39793.html

New Post(0)