Comparison between spring's annotation configuration and XML configuration

Source: Internet
Author: User

Note Configuration has a number of advantages over XML configuration:

It can take full advantage of the Java reflection mechanism to obtain the class structure information, which can effectively reduce the work of the configuration.

For example, when configuring an ORM map with JPA annotations, we do not need to specify the property name, type, etc. of the PO, and you do not even need to write task property mapping information if the Relationship table fields and PO property names and types are identical-because this information can be obtained through the Java reflection mechanism.

Comments and Java code reside in a file, and the XML configuration takes a separate configuration file, and most of the configuration information is not adjusted after the program has been developed.

If the configuration information is put together with Java code, it helps to enhance the cohesion of the program. In the case of a standalone XML configuration file, programmers often need to switch between program files and configuration files when writing a function, and this incoherent thinking can reduce development efficiency.

Therefore, in many cases, the annotation configuration is more popular than the XML configuration, and the annotation configuration has a tendency to be more popular. One of the big enhancements to Spring 2.5 is the introduction of a number of annotation classes, which you can now use to configure most of the XML configuration.

comment Configuration and XML configuration where applicable

With these IOC annotations, can we completely exclude the way the original XML is configured? The answer is in the negative. There are several reasons for this:

note configurations are not necessarily inherently superior to XML configurations. If the Bean's dependencies are fixed , such as what DAO classes the service uses, and this configuration information is not adjusted at deployment time , the annotation configuration is better than the XML configuration Conversely, if this dependency is adjusted at deployment time, the XML configuration is obviously better than the annotation configuration, because annotations are adjustments to the Java source code, and you need to rewrite the source code and recompile before you can implement the adjustment.

If the Bean is not a class written by itself (such as JdbcTemplate, Sessionfactorybean, etc.), the annotation configuration will not be implemented, and the XML configuration is the only available way.

note configurations tend to be class-level, while XML configurations can be more flexible. For example, transaction configurations using the Aop/tx namespace are more flexible and simple than @Transaction transaction annotations.

so in implementing applications, we often need to use both the annotation configuration and the XML configuration, and the annotation configuration is preferred for configuration at the class level without changes, whereas for those third-party classes and configurations that are prone to adjustment, the XML configuration should be preferred. Spring combines the meta-information of these two configurations before implementing bean creation and bean injection in concrete form.

advantages and disadvantages of XML configuration:

The advantages are:

1. The XML configuration method further reduces the coupling, making the application easier to extend , even if the configuration file is further modified and does not require engineering modification and recompilation.

2. When dealing with large volumes of traffic, it should be better to use XML configuration. Because XML is a clearer indication of the relationship between the various objects, calls between the various business classes. at the same time, the relevant configuration of spring can be at a glance.

Of course, some people will say that with XML configuration, the large volume of business will make the XML file too large, not easy to view. This allows us to write more than one XML configuration file with business decomposition.

Disadvantages are:

Configuration file reading and parsing takes a certain amount of time, too many configuration files are difficult to manage, the correctness of the configuration can not be verified, increasing the test difficulty.

advantages and disadvantages of annotation configuration:

The advantages are:

1. In the class file, the maintenance cost can be reduced, the annotation configuration mechanism is obviously simple

2. No third-party parsing tools are required, and Java reflection technology is used to complete the task

3. Editing period can verify correctness, error becomes easy

4. Improve development efficiency

Disadvantages are:

1. If you need to modify the annotation, then recompile the entire project

2. The relationship between business classes is not as easy to grasp as the XML configuration.

3. If more annotation in the program, directly affect the quality of code, the code of the brevity has a certain impact

Transferred from: https://www.cnblogs.com/yq12138/p/7210274.html

Comparison between spring's annotation configuration and XML configuration

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.