Author: Jiangnan Baiyi
1. Because the two have different purposes, annotation and XML should coexist.
Now that anootation vs XML is available, its respective uses are clear in the continuous demonstration of each side of spring and ejb3.
Although the two can completely replace each other, there is no absolutely impossible thing to do, but there is a good problem, the use of the two is different, should be used:
Parameters such as "size = 10, db_user = ito" are still suitable for XML, yaml (Ruby), properties, and INI files.
Annotation is part of the real program and is the one missing in the old programming system.
Let's look at the development of programming languages:
At first, it was just the function encapsulation;
In the OO language, the object has its own attributes and methods;
In the age when the framework was full, these attributes and methods had their own attributes to tell the framework how to provide services for itself. For example, in spring and ejb3, pojo uses configuration information to tell the framework to provide services without intrusion. The entire evolutionary process is very natural.
Because this information is a part of the program, it should still be put in the code. Spring splits it into XML, which significantly affects the code reading.
2. anotation/xml pk interface, Spring Non-invasive basis
Slesky's blog talked about Annotation/XML's interface PK. This PK can also be considered as a PK that spring has no intrusion to ejb2.1 in the framework.
In the ejb2.1 era, pojo must declare something to the framework through interfaces, which results in the framework's intrusion and forces pojo to implement a bunch of interfaces. Spring has moved the metadata to the xml configuration file.
You can give an example in spring. For example, after spring injects dependencies A and B into a pojo, it needs to assemble an internal attribute C according to A and B, so it needs to run another Init () function. Spring provides two methods: one is an invasive afterpropertiesset () function that implements the initializingbean interface for spring to call. The other is non-invasive. You can define the name of the init function in the bean XML node.
3. annotation's current weakness
First, the annotation of hibernate, struts, three aunts, and four aunts will be even worse if all the brains are piled up on the poor pojo.
Second, the expression capability of annoation is limited, and it is not as good as XML.
4. jsr250-common annotations for the Java
Final draft has been released. When gossip comes back, it just defines several common annotation.
1. generatedThe automatically generated code must be declared with the tool name, such as @ generated ("com. Sun. xml. rpc. aprocessor ")
2.Resource/ResourcesIt is the resource reference used in ejb3.
3. postconstruct/predestroyDeclare the functions such as question.
There are also some security aspects of annotation, don't care.
Article:
Java framework writing (1) -- Preface
Java framework programming (2)-boring usage of generics and platforms for wiping Methods
Java framework programming (3) -- fragment about reflection
Java framework programming (4) -- Annotation vs XML vs interface the latest round
Java framework programming (5) -- cglib Application
Framework programming in Java (6) -- ASM (to be written)