A comparison between spring's annotation configuration and XML configuration _java

Source: Internet
Author: User

The

annotation configuration has many advantages over XML configuration:
It takes full advantage of the Java reflection mechanism to get class-structured information that can effectively reduce the configuration's work. If you are using JPA annotations to configure ORM mappings, we do not need to specify information such as the property name, type, and so on of the PO, and if the Relationship table field and the Po property name and type are consistent, you do not even need to write task attribute mapping information--because the information can be obtained through the

Comments and Java code are in one file, and the XML configuration takes a separate configuration file, and most configuration information is not adjusted after the program is developed, and if the configuration information is put together with Java code, it helps to enhance the cohesion of the program. With a stand-alone XML configuration file, programmers often need to switch between program files and configuration files when writing a feature, which can reduce development efficiency.

Therefore, in many cases, annotation configurations are more popular than XML configurations, and annotation configurations are a further trend. One of the big enhancements to Spring 2.5 is the introduction of many annotation classes, and now you can use the annotation configuration to complete most of the XML configuration functionality.

annotation configuration and XML Configuration application Scenarios
With these IOC annotations, can we completely exclude the original XML configuration? The answer is in the negative. There are several reasons for this:
Annotation configurations are not necessarily inherently superior to XML configurations. If the Bean's dependencies are fixed, (such as which DAO classes the service uses), this configuration information will not be adjusted at deployment time, then 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 the annotation is Java Source code adjustment, you need to rewrite the source code and recompile before you can implement the adjustment.

If the Bean is not a class that is written by itself (such as JdbcTemplate, Sessionfactorybean, etc.), the annotation configuration will not be implemented, at which point the XML configuration is the only available method.

Annotation configurations are often class-level, and XML configurations can be more flexible. For example, the transaction configuration using the Aop/tx namespace is more flexible and simpler than @Transaction transaction annotations.

Therefore, in implementing applications, we often need to use annotation configuration and XML configuration at the same time, the annotation configuration can be prioritized for configuration with class level and not change, while for those third party classes and those that are prone to adjust, the XML configuration should be given priority. Spring merges the meta information of these two configurations before concrete implementation of the bean creation and bean injection.

Summary
Spring has strong support for annotation configurations after 2.1, and the annotation configuration feature is one of the biggest highlights of spring 2.5. Reasonable use of the annotation configuration of Spring 2.5 can effectively reduce the workload of the configuration and improve the cohesion of the program. But this does not mean that traditional XML configurations will go extinct, and that the XML configuration still has an irreplaceable place in the configuration of Third-party class beans, as well as in the configuration of content such as data sources, cache pools, persistence layer Operations template classes, transaction management, and so on.

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.