12 tips for configuring XML in Spring!

Source: Internet
Author: User

12 tips for configuring XML in Spring!


Post: 12 tips for Spring with XML! This article is well written and I hope to learn from you ~~ Make progress together !!
12 tips for configuring XML in Spring!

Spring is a powerful java program framework, which is widely used in java programs. It uses POJO to provide enterprise-level services. Spring uses dependency injection to obtain simple and effective testing capabilities. Spring beans, dependencies, and beans required by services are described in the configuration file. The configuration file is generally in XML format. However, XML configuration files are lengthy and not easy to use. In a large project that uses a large number of beans, it will become hard to read and control.



In this article, I will show you 12 best tips on Spring XML configuration files. Some of them have more practical significance than the best skills. Pay attention to other factors, such as the design of the domain model, which may affect the XML configuration. However, this article focuses more on the readability and controllability of XML configuration.



1. Avoid using automatic assembly

Spring can automatically assemble dependencies through bean class introspection, so that you do not have to explicitly describe bean attributes or constructor parameters. The bean property can be automatically assembled based on the live matching type of the property name. Constructor can be automatically assembled according to the matching type. You can even set automatic assembly for automatic detection, so that Spring will select an appropriate mechanism for you. See the following example:





Spring can automatically assemble dependencies through bean class introspection, so that you do not have to explicitly describe bean attributes or constructor parameters. The bean property can be automatically assembled based on the live matching type of the property name. Constructor can be automatically assembled according to the matching type. You can even set automatic assembly for automatic detection, so that Spring will select an appropriate mechanism for you. See the following example:



<Bean id = "orderService"

Class = "com. lizjason. spring. OrderService"

Autowire = "byName"/>



The attribute name of the OrderService class is used to match a bean instance in the container. Automatic assembly will silently save some type information and reduce confusion. However, because it sacrifices the intuition and maintainability of this configuration, you will not use it in actual projects. Many guides and statement materials boast it as a very cool feature of Spring without mentioning its shortcomings. In my opinion, like Spring's object pool, it has more commercial taste. It seems that the XML configuration file can be simplified, but it actually increases its complexity, especially when many beans have been defined in your large-scale project. Spring allows you to mix automatic and manual assembly, but this conflict makes XML configuration even more confusing.



2. Use naming rules

Like Java coding, it is useful for developers to understand XML configurations by using clear, descriptive, and consistent naming rules in projects. Taking bean ID as an example, you can follow the naming rules of attributes in Java classes. For example, the bean ID of OrderServiceDAO should be orderServiceDAO. For large projects, add the package name before the bean ID as the prefix.



3. Simplified format

Simplified format helps reduce redundancy because it uses attribute values and references as attributes rather than child elements. See the following example:

<Bean id = "orderService"
Class = "com. lizjason. spring. OrderService">
        &

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.