Framework programming in Java (4)-annotation vs XML vs interface latest round

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.