(This article is issued in Java EMAG second phase) What is metadata programming
What is metadata, metadata is data to describe data (Data About Data). The most obvious example is XML Schema, XML Schema is a data describing XML, so it is metadata. Another example is a database, such as we can query a few tables in the database, what fields have each table, which is metadata.
In the world developed, metadata is an additional information that can be bound to a class, static or run time. JCR175 provides us with Annotation is a metadata.
However, before this, the metadata we have used is XML. If it is an EJB XML release descriptor, you need to define transaction properties based on each method. When the application server is guided, where it starts, hangs or submits a transaction, because you have defined in the metadata in the XML configuration file in Bean: Required, RequiresNew, Support, etc., they are bound to yours EJB class and transaction management. XDoclet is an example of another metadata.
Significance and simple example of Annotation
JDK1.5 does the Annotation relationship with the Classes, Fieldss and Methods we are common. As follows: If the classes and data members are nouns, the method is the verbs, then Annotation is adjective or an adverb, which describes the properties of their properties.
Ok, now come now to realize an Annotation
Import java.lang.annotation.ReTENTION;
Package Sample.Anotation;
@Retention (java.lang.annotation.Runtime)
Public @INTERFACE BROKER {
String name ();
String address ();
}
}
Use this Annotation
Import sample.Annotation.broker;
@Broker (name = "Anders", address = "
xiamen
")
Public class agent {
Public string gettelphone () {
RETURN "010-0592-2519280";
}
}
I get this Annotation
Public class
Main
{
Public static void main (String [] args) {
Agent agent = new agent ();
Try {
ANNOTATION [] a = agent.getclass (). GetMethod ("getBrokername"). GetanNotations ();
For (int i = 0; i IF (a [i] instanceof brroker { Broker Broker = (Broker) a [i]; System.out.println (Broker.Name ()); } } } Catch (Exception E) { E.PrintStackTrace (System.out); } } } ANNOTATION Class file format Using Sun's Java, we can see the representation of Annotation in the Class file. The following is a comparison result: Source code: @Retention (java.lang.annotation.RuntionPolicy.Runtime) public @Interface broker { String name (); String address (); } JavaP results: Compiled from "Broker.java" Interface Broker Extends Java.lang.Annotation.Annotation Sourcefile: "Broker.java" Minor Version: 0 Major Version: 0 Constant pool: Const # 1 = Class # 9; // Broker Const # 2 = Class # 10; // Object Const # 3 = Class # 11; // annotation Const # 4 = asciz name; Const # 5 = asciz () Ljava / Lang / String ;; Const # 6 = Asciz Address; Const # 7 = Asciz SourceFile; Const # 8 = asciz broker.java; Const # 9 = Asciz Broker; Const # 10 = Asciz Java / Lang / Object; Const # 11 = asciz java / lang / annotation / annotation; { Public Abstract Java.lang.String Name (); Public Abstract Java.lang.String Address (); } Source code: Public interface broker { String name (); String address (); } JavaP results: Compiled from "Broker.java" Interface Broker Sourcefile: "Broker.java" Minor Version: 0 Major Version: 0 Constant pool: Const # 1 = Class # 8; // Broker Const # 2 = Class # 9; // Object Const # 3 = asciz name; Const # 4 = asciz () Ljava / Lang / String ;; Const # 5 = Asciz Address; Const # 6 = asciz sourcefile; Const # 7 = asciz broker.java; Const # 8 = Asciz Broker; Const # 9 = Asciz Java / Lang / Object; { Public Abstract Java.lang.String Name (); Public Abstract Java.lang.String Address (); } Source code: @Broker (name = "Anders", address = " xiamen ") Public class agent { Public string gettelphone () { RETURN "0592-2519580"; } } JavaP results: Compiled from "age.java" public class agent extends java.lang.object Sourcefile: "Agent.java" RuntimevisiBeaNotations: length = 0x10 00 01 00 11 00 02 00 12 73 00 13 00 14 73 00 15 Minor Version: 0 Major Version: 0 Constant pool: Const # 1 = Method # 4. # 22; // java / lang / object. " Const # 2 = String # 23; // 0592-2519580 Const # 3 = Class # 24; // Agent Const # 4 = Class # 25; // Object Const # 5 = asciz Const # 6 = asciz () V; Const # 7 = Asciz Code; Const # 8 = Asciz Linenumbertable; Const # 9 = Asciz LocalvariableTable; Const # 10 = asciz this; Const # 11 = Asciz Lagent ;; Const # 12 = asciz gettelphone; Const # 13 = asciz () Ljava / Lang / String ;; Const # 14 = asciz sourcefile; Const # 15 = Asciz Agent.java; Const # 16 = asciz runtimevisibleannotation; Const # 17 = asciz lbroker ;; Const # 18 = asciz name; Const # 19 = asciz anders; Const # 20 = Asciz Address; Const # 21 = Asciz xiamen ; Const # 22 = nameandType # 5: # 6; // " Const # 23 = asciz 0592-2519580; Const # 24 = Asciz Agent; Const # 25 = Asciz Java / Lang / Object; // The following is the method domain, slightly Source code: Public class agent { Public string gettelphone () { RETURN "0592-2519580"; } } JavaP results: Compiled from "Agent.java" Public class agent extends java.lang.Object Sourcefile: "Agent.java" Minor Version: 0 Major Version: 0 Constant pool: Const # 1 = method # 4. # 16; // java / lang / object. " Const # 2 = String # 17; // 0592-2519580 Const # 3 = Class # 18; // AgentConst # 4 = Class # 19; // Object Const # 5 = asciz Const # 6 = asciz () V; Const # 7 = Asciz Code; Const # 8 = Asciz Linenumbertable; Const # 9 = Asciz LocalvariableTable; Const # 10 = asciz this; Const # 11 = Asciz Lagent ;; Const # 12 = asciz gettelphone; Const # 13 = asciz () Ljava / Lang / String ;; Const # 14 = asciz sourcefile; Const # 15 = Asciz Agent.java; Const # 16 = nameAndType # 5: # 6; // " Const # 17 = asciz 0592-2519580; Const # 18 = Asciz Agent; Const # 19 = Asciz Java / Lang / Object; // The following is the method domain, slightly Supplementary description: We all know that when Java 1.0 is released, the format of Java Class File has been set, and it is to be described in order to design the property to design the properties in Java Class File. It has not changed until J2SE1.4. But this time, in order to better support Metadata technology, a total of 8 properties have been added. They are: · "EnclosingMethod" Attribute: Anonymous Class or Local Inner Class Use this attribute to describe the Class's Scope. • "Signature" Attribute: Generals Class, Method, or FileD uses this attribute to record the type of type, because Java's model uses a wiping method. · "LocalVariableTypeTable" Attribute: mainly to the debugger uses, objectives, and "LocalVariableTable" Attribute similar, but the parameter list "LocalVariableTable" Attribute to be recorded, and "LocalVariableTypeTable" Attribute parameters of record type. · "RuntimevisibleanNotations" Attribute: Determines the Annotation to be returned by the API of the Reflection, Applicable Objects: Class, Method, Field · "RuntimeInvisibleanNotations" Attribute: Make sure the Annotation cannot be returned by the Reflection's API, apply object: Class, Method, Field. · "RuntimeVisibleParameterAnnotations" Attribute: Attribute with "RuntimeVisibleAnnotations" Suitable for: Method, (the Method parameters · "RuntimeInvisibleParameterAnnotations" Attribute: Method, (the parameters of the Method: Attribute, Applicable with "RuntimeInvisibleAnnotations." · "AnnotationDefault" Attribute: Applicable object: Method, record the default value. Why do I need annotation? We have widely used another metadata XML before ANNOTATION, why need Annotation. Annotation and XML as the difference between metadata - position. ANNOTATION writes in the source code, while XML is written outside. Why is this? If you have developed EJB, you must write the XML description file for your EJB. When a large number of EJBs need to describe, so-called "Descriptor Hell" appears. This also led to the emergence of famous XDoclets. ANNOTATION can avoid this Descriptor Hell. In addition, you change a method to increase or decrease a parameter, you don't have to modify the XML file, and you don't have to use ANNOTATION. Using Annotation will make development and deployment, provide development efficiency. In addition: Another problem with XML is: a lot of XML configuration is too verbose. Compared with EJB and Hibernate or WebWork can be obviously discovered. ANNOTATION In EJB3, Annotation combines the development and deployment of work. However, in some enterprise environments, developers do not control such as data source names (these are the work of deployment departments and management), so that the data source name is better in XML. Annotation is still static, once added or modified Annotation, you need to recompile, read at runtime, which loses the ability to run time configuration. Therefore, ANNOTATIONS will not replace XML, which only provides another way. And I believe that Sun will provide a way in the future to change Metadata at runtime. About this TSS has a very intense discussion, many developers have proposed: using XML to change the Annotation, and hope that similar ways can be adopted as standard specifications. For example, use the following format: @Entity @Table (Name = "Auction_Item") @TX (Requires_New) Method> clas> Of course there are different opinions: But then, I think of "overriding" as a different problem to "xml deployment descriptors", and so I think we need two solutions I think Cedric and Bill are trying to kill two birds with one stone, so. Maybe Their Proposals Are Better .... About providing dynamic configuration capabilities for Annotation, one of them thinks: Sun Make It Real pain to do the deployment XML SO That The DEPLOMENT XML SO That THE CAN Introduce Annotation To FIX It. The Annotation Can make Code / Deployment Coupling SO STRONG THAT SUN COME OUT WITH WITH A New Way (Annotation Interceptor in JDK 1.6? :)) for fixing it. And the cycles goes on ... This reminds me of similar phenomena: JSP and Taglib. I hope Annotation will not have the same fate with Taglib. Annotation itself introduces another type of interface. In EJB3, it does make the program more POJO and eliminate some interfaces. And the compiled code can also be ported directly to another environment that does not process these Annotations (thanks to the VM does not check those ANNOTATION Classes, even these classes are not in classpath). However, the code has indeed added another interface. This performance is compiled, and if there is no these annotation classes, it is compiled. Another problem (it is still very important), about Annotation's Namespace. In the system of multi-layer applications, the same global Annotation's NameSpace conflict may occur. For example, some public annotations, such as @Transaction, will be applied in many hierarchies. Try to make NameSpace, you can avoid this problem. Metadata programming: Annotation has been integrated into a lot of Java specifications and standards, it is important that it has been used by J2EE, such as EJB3, of course, is also many open source components such as: aspectj. The most important application of Annotation will be AOP: Since Annotation can naturally represent another cross-faced surface in the system, the Annotation recognition is obtained by reflection, so Annotation is naturally applied to a dynamic agent-based AOP implementation. AOP-Alliance also supports metadata handling. Aspectj also released a new version based on ANNOTATION. On the AOP, using Annotation has an advantage than using XML: As mentioned earlier, Annotation is more like adjective and adverb, which is not easy to verbose. Of course, this is relative, relying more on the efforts of developers in actual implementation. Here, the author will show an incomplete Nnotation-based AOP example code - an example of a bank card. Functional requirements: Bank card business is divided into transfer, query balance, query details, POS spending, etc. This transfer and POS consumption is to charge (transfer charge is the user's handling fee, while POS spending is a merchant's handling fee), and the POS consumption may also have points (such as the author's peony.). Consumption transfer must be recorded. However, the query balance does not need to record down. The code is as follows (in this example, there is no use of the dynamic agent to use the existing AOP frame, so that the code looks simple) / / Bank card object PUBLIC class card { PRIVATE STRING ACCOUNT; // Some Field Method } ANNOTATION: // fee // type = "user", indicating that the user's handling fee is charged; type = "biz", indicating that the merchant handling fee is Public @Interface feed String type (); } // points Public @Interface index { } // Record details Public @Interface bizlog { } // Transaction Public @Interface Transaction { } // Business interface Public interface bizaction { Void Execute (Card Card, Rundata Rundata); } // Transfer business @Fee (type = "user") @Transaction @Bizlog Public class transferaction implements bizaction { Public void Execute (Card Card, Rundata Rundata) { // TO CHANGE BODY OF IMPLEMENTED METHODS USE FILE | Settings | File Templates. } } // POS spending @Fee (type = "biz") @Transaction @Bizlog @Index Public class posaction imports bizaction { Public void Execute (Card Card, Rundata Rundata) { // TO CHANGE BODY OF IMPLEMENTED METHODS USE FILE | Settings | File Templates. } } // query details Public class querydetail imports bizaction { Public void Execute (Card Card, Rundata Rundata) { // TO CHANGE BODY OF IMPLEMENTED METHODS USE FILE | Settings | File Templates. } } // Business Operation Monitor Interface Public interface bizactionmonitor { Void Execute (BizAction Action, Rundata); } // Business Operation Monitor Implementation Public Class BizActionMonitorImpl Implements BizActionMonitor { Public void Execute (BizAction Action, Rundata Rundata) { Annotation [] Annotations = action.getClass (). GetanNotations (); for (Annotation Annotation: Annotation) { IF (Annotation InstanceOf Fee) {// Computing Fees} IF (Annotation InstanceOf Index) {// Computing Points} IF (Annotation InstanceOf Transaction) {// Prepare Transaction} IF (Annotation InstanceOf Bizlog) {// Record Detail} } } } // Controller object Public class controller { Private BizactionMonitor Monitor; Public void execute (bizactionui, rundata) { Bizaction action = getAction (bizactionui); Monitor.execute (Action, Rundata); } } // runtime data Public interface rundata { // Some Method } // User Equipment (POS, ATM or Counter Terminal) Public class bizactionui { PRIVATE RUNDATA RUNDATA; Private Controller Controller; Public BizActionui (Rundata Rundata, Controller Controller) { this.Rundata = Rundata; This.Controller = Controller; } Public void execute () {// a subclass implementation} Public void commit () { Controller.execute (this, rundata); } } Public class Main { PRIVATE RUNDATA RUNDATA; Private Controller Controller; Public populateui (commist) { BizActionui UI = Getui (Command); Ui.execute (); } Public BizActionui getui (Command) { // ... BizActionui UI IF (// ....) { UI = new SomebizActionUi (Rundata, Controller); } Return UI; } Public static main (String [] args) { // ... Main main = new Main (); Main.populateui (Command) // ... } } Conclusion: This article discusses Annotation technology, showing the Class file format of Annotation, and discusses the advantages and insufficient advantages of Annotation technology itself, and compares the existing XML technology to look forward to the application prospect of ANNOTATION technology. Limited to the level of the author (including the level of technical level and writing), the technology is relatively limited, and the writing is also a lot of words in writing. Therefore, the code this article will be more (about more than half).